0
select (select sc_machid as ClinicID,
case
    when substring(sc_machid,1,3)='BB0' then 'Head Office' 
    WHEN substring(sc_machid,1,3)='BA0' THEN 'HeadOffice'
    when substring(sc_machid,1,3)='BA1' THEN 'Villiers'
    when substring(sc_machid,1,3)='BA2' then 'Imperial IDC'
    WHEN substring(sc_machid,1,3)='BA3' THEN 'Warden'
    WHEN substring(sc_machid,1,3)='BA4' THEN 'Harrismith'
    WHEN substring(sc_machid,1,3)='BA5' THEN 'Tugela'
    WHEN substring(sc_machid,1,3)='BA6' THEN 'MooiRiver'
    WHEN substring(sc_machid,1,3)='bad' THEN 'MooiRiver'
    WHEN substring(sc_machid,1,3)='BB0' THEN 'Port Elizabeth'
    WHEN substring(sc_machid,1,3)='BB1' THEN 'East London'
    when substring(sc_machid,1,3)='BB2' THEN 'Umthatha'
    when substring(sc_machid,1,3)='BB3' then 'Kokstad'
    WHEN substring(sc_machid,1,3)='BB4' THEN 'Marian Hill'
    WHEN substring(sc_machid,1,3)='BB5' THEN 'Zeerust'
    WHEN substring(sc_machid,1,3)='BC0' THEN 'Imperial SBD'
    WHEN substring(sc_machid,1,3)='BC1' THEN 'Imperial TSD'
    WHEN substring(sc_machid,1,3)='BC2' THEN 'Roodekop'
    WHEN substring(sc_machid,1,3)='BD0' THEN 'Ventersburg'
    WHEN substring(sc_machid,1,3)='BD1' THEN 'Colesburg'
    WHEN substring(sc_machid,1,3)='BD2' THEN 'Hanover'
    WHEN substring(sc_machid,1,3)='BD3' THEN 'Beaufort West'
    WHEN substring(sc_machid,1,3)='BD4' THEN 'Beit Bridge Border Post Musina'
    WHEN substring(sc_machid,1,3)='BD5' THEN 'Cape Town - Epping'
    WHEN substring(sc_machid,1,3)='BD7' THEN 'Komati Poort'
    end as ClinicName, count(TransactionType) as NumberOfFirstTimePatientVisits
    from sc_audit 
    where TransactionType = 'Insert Patient Record' 
    group by ClinicName)
as t1,
(
    select pat_id as PatientID,
    count(*) as NumberOfVisits,
    sc_clinicid as ClinicID,
    CASE
        WHEN sc_clinicid = 'BA0' THEN 'HeadOffice'
        when sc_clinicid = 'BA1' THEN 'Villiers'
        when sc_clinicid = 'BA2' then 'Imperial IDC'
        WHEN sc_clinicid = 'BA3' THEN 'Warden'
        WHEN sc_clinicid = 'BA4' THEN 'Harrismith'
        WHEN sc_clinicid = 'BA5' THEN 'Tugela'
        WHEN sc_clinicid = 'BA6' THEN 'MooiRiver'
        WHEN sc_clinicid = 'bad' THEN 'MooiRiver'
        WHEN sc_clinicid = 'BB0' THEN 'Port Elizabeth'
        WHEN sc_clinicid = 'BB1' THEN 'East London'
        when sc_clinicid = 'BB2' THEN 'Umthatha'
        when sc_clinicid = 'BB3' then 'Kokstad'
        WHEN sc_clinicid = 'BB4' THEN 'Marian Hill'
        WHEN sc_clinicid = 'BB5' THEN 'Zeerust'
        WHEN sc_clinicid = 'BC0' THEN 'Imperial SBD'
        WHEN sc_clinicid = 'BC1' THEN 'Imperial TSD'
        WHEN sc_clinicid = 'BC2' THEN 'Roodekop'
        WHEN sc_clinicid = 'BD0' THEN 'Ventersburg'
        WHEN sc_clinicid = 'BD1' THEN 'Colesburg'
        WHEN sc_clinicid = 'BD2' THEN 'Hanover'
        WHEN sc_clinicid = 'BD3' THEN 'Beaufort West'
        WHEN sc_clinicid = 'BD4' THEN 'Beit Bridge Border Post Musina'
        WHEN sc_clinicid = 'BD5' THEN 'Cape Town - Epping'
        WHEN sc_clinicid = 'BD7' THEN 'Komati Poort'
    end as ClinicName 
    from sc_queue 
    group by ClinicName
) 
as t2;

上面的查询给了我一个错误,上面写着

Operand should contain 1column(s)

请帮助我,我似乎无法弄清楚问题出在哪里

4

3 回答 3

0

在 t2 中,您选择了多个列,然后将别名分配给错误的列。尝试选择一列它将起作用。问题是您选择多列作为一列。

select (select sc_machid as ClinicID,
case
    when substring(sc_machid,1,3)='BB0' then 'Head Office' 
    WHEN substring(sc_machid,1,3)='BA0' THEN 'HeadOffice'
    when substring(sc_machid,1,3)='BA1' THEN 'Villiers'
    when substring(sc_machid,1,3)='BA2' then 'Imperial IDC'
    WHEN substring(sc_machid,1,3)='BA3' THEN 'Warden'
    WHEN substring(sc_machid,1,3)='BA4' THEN 'Harrismith'
    WHEN substring(sc_machid,1,3)='BA5' THEN 'Tugela'
    WHEN substring(sc_machid,1,3)='BA6' THEN 'MooiRiver'
    WHEN substring(sc_machid,1,3)='bad' THEN 'MooiRiver'
    WHEN substring(sc_machid,1,3)='BB0' THEN 'Port Elizabeth'
    WHEN substring(sc_machid,1,3)='BB1' THEN 'East London'
    when substring(sc_machid,1,3)='BB2' THEN 'Umthatha'
    when substring(sc_machid,1,3)='BB3' then 'Kokstad'
    WHEN substring(sc_machid,1,3)='BB4' THEN 'Marian Hill'
    WHEN substring(sc_machid,1,3)='BB5' THEN 'Zeerust'
    WHEN substring(sc_machid,1,3)='BC0' THEN 'Imperial SBD'
    WHEN substring(sc_machid,1,3)='BC1' THEN 'Imperial TSD'
    WHEN substring(sc_machid,1,3)='BC2' THEN 'Roodekop'
    WHEN substring(sc_machid,1,3)='BD0' THEN 'Ventersburg'
    WHEN substring(sc_machid,1,3)='BD1' THEN 'Colesburg'
    WHEN substring(sc_machid,1,3)='BD2' THEN 'Hanover'
    WHEN substring(sc_machid,1,3)='BD3' THEN 'Beaufort West'
    WHEN substring(sc_machid,1,3)='BD4' THEN 'Beit Bridge Border Post Musina'
    WHEN substring(sc_machid,1,3)='BD5' THEN 'Cape Town - Epping'
    WHEN substring(sc_machid,1,3)='BD7' THEN 'Komati Poort'
    end as ClinicName, count(TransactionType) as NumberOfFirstTimePatientVisits
    from sc_audit 
    where TransactionType = 'Insert Patient Record' 
    group by ClinicName)
as t1,
(
    CASE
        WHEN sc_clinicid = 'BA0' THEN 'HeadOffice'
        when sc_clinicid = 'BA1' THEN 'Villiers'
        when sc_clinicid = 'BA2' then 'Imperial IDC'
        WHEN sc_clinicid = 'BA3' THEN 'Warden'
        WHEN sc_clinicid = 'BA4' THEN 'Harrismith'
        WHEN sc_clinicid = 'BA5' THEN 'Tugela'
        WHEN sc_clinicid = 'BA6' THEN 'MooiRiver'
        WHEN sc_clinicid = 'bad' THEN 'MooiRiver'
        WHEN sc_clinicid = 'BB0' THEN 'Port Elizabeth'
        WHEN sc_clinicid = 'BB1' THEN 'East London'
        when sc_clinicid = 'BB2' THEN 'Umthatha'
        when sc_clinicid = 'BB3' then 'Kokstad'
        WHEN sc_clinicid = 'BB4' THEN 'Marian Hill'
        WHEN sc_clinicid = 'BB5' THEN 'Zeerust'
        WHEN sc_clinicid = 'BC0' THEN 'Imperial SBD'
        WHEN sc_clinicid = 'BC1' THEN 'Imperial TSD'
        WHEN sc_clinicid = 'BC2' THEN 'Roodekop'
        WHEN sc_clinicid = 'BD0' THEN 'Ventersburg'
        WHEN sc_clinicid = 'BD1' THEN 'Colesburg'
        WHEN sc_clinicid = 'BD2' THEN 'Hanover'
        WHEN sc_clinicid = 'BD3' THEN 'Beaufort West'
        WHEN sc_clinicid = 'BD4' THEN 'Beit Bridge Border Post Musina'
        WHEN sc_clinicid = 'BD5' THEN 'Cape Town - Epping'
        WHEN sc_clinicid = 'BD7' THEN 'Komati Poort'
    end as ClinicName 
    from sc_queue 
    group by ClinicName
) 
as t2,
(select pat_id as PatientID,        
    from sc_queue 
    group by ClinicName) as t3,
(select count(*) as NumberOfVisits,        
    from sc_queue 
    group by ClinicName) as t4,
(select sc_clinicid as ClinicID,        
    from sc_queue 
    group by ClinicName) as t5;
于 2013-06-03T09:35:30.467 回答
0

子查询SELECT sc_machid as ClinicID, ...产生三列 ( ClinicID, ClinicName, NumberOfFirstTimePatientVisits)。因此,它不能是SELECT表达式的一部分。它必须是FROM条款的一部分。像这样做:

SELECT t1.ClinicID, t1.ClinicName, t1.NumberOfFirstTimePatientVisits
FROM (SELECT sc_machid as ClinicID, ... ) as t1 ...

也一样select pat_id as PatientID,count(*) as NumberOfVisits ...。结合这两个使用

SELECT
    t1.ClinicID, t1.ClinicName, t1.NumberOfFirstTimePatientVisits,
    t2.PatientID, t2.NumberOfVisits, t2.ClinicID, t2.ClinicName
FROM
    (
        SELECT
            sc_machid AS ClinicID,
            CASE
                WHEN SUBSTRING (sc_machid,1,3)='BB0' THEN 'Head Office'
                -- ...
            END AS ClinicName,
            COUNT(TransactionType) AS NumberOfFirstTimePatientVisits
        FROM sc_audit 
        WHERE TransactionType = 'Insert Patient Record' 
        GROUP BY ClinicName
    ) AS t1,
    (
        SELECT
            pat_id AS PatientID,
            count(*) AS NumberOfVisits,
            sc_clinicid AS ClinicID,
            CASE
                WHEN sc_clinicid = 'BA0' THEN 'HeadOffice'
                -- ...
            END AS ClinicName
        FROM sc_queue
    ) AS t2

SELECT如果子查询返回包含单个列的单个记录,则子查询只能是表达式的一部分。如果它返回具有多列的单个记录,您将收到错误

#1242 - 子查询返回多于 1 行

于 2013-06-03T09:03:14.800 回答
0

您的 t2 列返回了多个PatientID, NumberOfVisits, sc_clinicid....不允许的记录。当您将该查询添加为列结果时,您只能在内部查询中添加 1 列。

检查这个小提琴我试图在查询FIDDLE中解决你的错误

于 2013-06-03T09:28:17.947 回答