2012-10-25 115 views
-1

我在分組中遇到錯誤...請幫助。我的SQL代碼如下。我試圖讓醫生名字的第1列,2列醫保患者數,3列非醫保患者不能在用於GROUP BY子句的group by列表的表達式中使用聚合或子查詢

SELECT DoctorListName, 
(CASE WHEN InsuranceCarrierName IN ('Humana Medicare','Medicare','Humana Gold Plus Medicare') 
THEN count(uvPatientInsurance.InsuranceCarrierName) END) as CountMedicare, 
(CASE WHEN InsuranceCarrierName NOT IN ('Humana Medicare','Medicare','Humana Gold Plus Medicare') 
THEN count(uvPatientInsurance.InsuranceCarrierName) END) as CountNOTMedicare 

FROM (STPN.dbo.vSelectPatient vSelectPatient 
INNER JOIN STPN.dbo.uvPatientInsurance uvPatientInsurance ON vSelectPatient.PatientId=uvPatientInsurance.PatientId) 
INNER JOIN STPN.dbo.uvVisit uvVisit ON vSelectPatient.PatientId=uvVisit.PatientId 

WHERE vSelectPatient.PatientStatusMId=-900 
AND (uvVisit.Entered>={ts '2011-01-01 00:00:00'} 
AND uvVisit.Entered<{ts '2012-09-30 00:00:01'}) 
GROUP BY DoctorListName, 
(CASE WHEN InsuranceCarrierName IN ('Humana Medicare','Medicare','Humana Gold Plus Medicare') 
THEN count(uvPatientInsurance.InsuranceCarrierName) END), 
(CASE WHEN InsuranceCarrierName NOT IN ('Humana Medicare','Medicare','Humana Gold Plus Medicare') 
THEN count(uvPatientInsurance.InsuranceCarrierName) END) 
+1

哪個SQL? TSQL,PLSQL或MySQL? –

+0

沒關係....知道了 – user1774975

+2

@ user1774975:然後請澄清你的問題,併發布答案,以便它可以幫助他人。 –

回答

0

Normaly,使用COUNT或聚合函數的時候,你需要指定一個GROUP BY條款...只是像錯誤說:

SELECT DoctorListName, 
(CASE WHEN InsuranceCarrierName IN ('Humana Medicare','Medicare','Humana Gold Plus Medicare') 
THEN count(uvPatientInsurance.InsuranceCarrierName) END) as CountMedicare, 
(CASE WHEN InsuranceCarrierName NOT IN ('Humana Medicare','Medicare','Humana Gold Plus Medicare') 
THEN count(uvPatientInsurance.InsuranceCarrierName) END) as CountNOTMedicare 

FROM (STPN.dbo.vSelectPatient vSelectPatient 
INNER JOIN STPN.dbo.uvPatientInsurance uvPatientInsurance ON vSelectPatient.PatientId=uvPatientInsurance.PatientId) 
INNER JOIN STPN.dbo.uvVisit uvVisit ON vSelectPatient.PatientId=uvVisit.PatientId 

WHERE vSelectPatient.PatientStatusMId=-900 
AND (uvVisit.Entered>={ts '2011-01-01 00:00:00'} 
AND uvVisit.Entered<{ts '2012-09-30 00:00:01'}) 
GROUP BY DoctorListName 

GROUP BY子句只能使用列,沒有聚集功能,如COUNT

相關問題