2013-06-03 52 views
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; 

上述查詢給我,說操作數應包含1column(S)

Operand should contain 1column(s) 

請幫我一個錯誤,我似乎無法找出問題的所在

+0

它看起來像你的內部選擇應該是第一個選擇的參數,而不是選擇的列。 – Lidor

+0

這是什麼樣的查詢? – diEcho

+0

你應該爲這兩個case語句創建一個[function](http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html),假設它們實際上應該是相同的。另外,你可以說'CASE sc_clinicid WHEN ...'並且隨處移除'sc_clinicid ='。 – Dukeling

回答

0

子查詢SELECT sc_machid as ClinicID, ...產生三列(ClinicID,ClinicNameNumberOfFirstTimePatientVisits)。因此,它不能是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行

+0

請注意,這隻能修復你的語法錯誤。我想你也想添加'WHERE'條件't1.ClinicID = t2.ClinicID'。 – Oswald

0

你列T2將返回一個以上的記錄PatientID, NumberOfVisits, sc_clinicid....這是不允許的。當您將該查詢添加爲列結果時,您可以在內部查詢中僅添加1列。

入住這撥弄我試圖INACT您的錯誤在查詢FIDDLE

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; 
相關問題