使用分区依据时,case语句创建的列不会分组

时间:2017-10-22 15:20:07

标签: sql-server pivot case

我正在报告我之前遇到过相同问题的问题,但解决了here is the link to question

在这里,我遇到了同样的问题,这里的变化不大,我还要求分区

CASE WHEN MP.PT = 'OPD' THEN COUNT(MP.YN) OVER (PARTITION BY  MP.PT,mp.tti ORDER BY  mp.tti)   END AS OPD,
CASE WHEN MP.PT = 'IPD' THEN COUNT(MP.YN) OVER (PARTITION BY  MP.PT,mp.tti ORDER BY  mp.tti)   END AS IPD,
CASE WHEN MP.PT = 'A&E' THEN COUNT(MP.YN) OVER (PARTITION BY  MP.PT,mp.tti ORDER BY  mp.tti)   END AS EAC

结果应与单个选择的一部分位于同一行,但它出现在多行而不会聚合

1 个答案:

答案 0 :(得分:1)

试试这个:

SUM(IIF(MP.PT = 'OPD', 1, 0)) OVER (PARTITION BY mp.tti)  AS OPD,
SUM(IIF(MP.PT = 'IPD', 1, 0)) OVER (PARTITION BY mp.tti)  AS IPD,
SUM(IIF(MP.PT = 'A&E', 1, 0)) OVER (PARTITION BY mp.tti)  AS EAC