-1

我的分组出现错误...请帮助。我的 SQL 代码如下。我正在尝试获取 1 列医生姓名,2 列医疗保险患者计数,3 列非医疗保险患者

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)
4

1 回答 1

0

通常,在使用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.

于 2012-10-25T17:25:38.600 回答