按
计算列组 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和显示计数
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
但这最终会给我一个计数。我想显示每列的计数列
答案 0 :(得分:0)
SELECt [cityID],
COUNT([SCity]), SUM([SCity]),
COUNT([MCity]), SUM([MCity]),
COUNT([BCity]), SUM([BCity]),
[Flag]
FROM Table1 GROUP BY [cityID], [Flag]
<强> SAMPLE FIDDLE 强>