计数列分组
cityID, SCity, MCity, BCity, Flag
10, null, 400, null, 0
12 350, null, null, 1
13 600, null, null, 1
13 700, null, null, 1
15 null, null, 250, 3
预期结果,按 CityID 分组,将 MCity 和 City 旁边的显示计数相加
cityID, SCitycount, SCity, MCitycount, MCity, Bcitycount, BCity, Flag
10, 0, null, 1, 400 0, null, 0
12 1, 350, 0, null 0, null, 1
13 2, 1100, 0, null 0, null, 1
15 1, null, 0, null 1, 250, 3
这是我到目前为止所拥有的
SELECT
cityID,
SUM(CASE WHEN Flag = 1 THEN SCity END) AS SCity,
SUM(CASE WHEN Flag = 0 THEN MCity END) AS MCity,
SUM(CASE WHEN Flag = 3 THEN Bity END) AS BCity,
COUNT(*) as count
FROM #FINALRESULTS
GROUP BY cityID
但这最后会给我一个计数。我喜欢显示每列的计数列