包裹整个
CASE
聚合函数内部的表达式
COUNT
,然后删除
iPatient_typ
来自
GROUP BY
条款:
SELECT
iBranch_num,
COUNT(CASE WHEN iPatient_typ=1 THEN iPatient_num ELSE 0 END) AS [New Patient],
COUNT(CASE WHEN iPatient_typ=2 THEN iPatient_num ELSE 0 END) AS [Buying Patient],
COUNT(CASE WHEN iPatient_typ=3 THEN iPatient_num ELSE 0 END) AS [Active Patient],
COUNT(CASE WHEN iPatient_typ=4 THEN iPatient_num ELSE 0 END) AS [Inactive Patient]
FROM tblsotransaction
WHERE iStatus_typ = 1
AND iApply_dt BETWEEN 20130401 AND 20130408
AND iBranch_num = 14
GROUP BY iBranch_num;
或者:
SUM
:
SELECT
iBranch_num,
SUM(CASE WHEN iPatient_typ = 1 THEN 1 ELSE 0 END) AS [New Patient],
SUM(CASE WHEN iPatient_typ = 2 THEN 1 ELSE 0 END) AS [Buying Patient],
SUM(CASE WHEN iPatient_typ = 3 THEN 1 ELSE 0 END) AS [Active Patient],
SUM(CASE WHEN iPatient_typ = 4 THEN 1 ELSE 0 END) AS [Inactive Patient]
FROM tblsotransaction
WHERE iStatus_typ = 1
AND iApply_dt BETWEEN 20130401 AND 20130408
AND iBranch_num = 14
GROUP BY iBranch_num;