2016-07-11 76 views
0

我想如果條件是真還是假分裂基於列和計算的患者的數量在每個類別中每個醫生拆分列有條件

這是我的代碼:

SELECT p.MRP_CP_ID, COUNT(p.PTNT_ID) FROM PATIENT p 
JOIN PATIENT_SCHEDULE ps ON ps.PTNT_ID = p.PTNT_ID 
WHERE ps.MLTPL_PHRM_FLG = 0 
GROUP BY MRP_CP_ID; 

但我想在另一列顯示MLTPL_PHRM_FLG爲1的患者人數。是否有捷徑可尋?

​​

+0

請提供具有代表性的測試數據集和所需的結果。 –

回答

0

這可以通過有條件的聚集來完成。

SELECT p.MRP_CP_ID, 
COUNT(*) Total, 
COUNT(CASE WHEN ps.MLTPL_PHRM_FLG = 0 then 1 END) as PHRM_FLG_0_Counts, 
COUNT(CASE WHEN ps.MLTPL_PHRM_FLG = 1 then 1 END) as PHRM_FLG_1_Counts 
FROM PATIENT p 
JOIN PATIENT_SCHEDULE ps ON ps.PTNT_ID = p.PTNT_ID 
GROUP BY MRP_CP_ID;