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)
请帮助我,我似乎无法弄清问题在哪里
答案 0 :(得分: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行
答案 1 :(得分:0)
您的列t2返回多个不允许的记录PatientID, NumberOfVisits, sc_clinicid....
。当您将该查询添加为列结果时,您只能在内部查询中添加1列。
检查这个小提琴我试图在查询中发现您的错误 FIDDLE
答案 2 :(得分: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;