我有一个难以进行的查询=)
停止表格包含以下列
Id, Area, SubArea, Date, StopNumber, DownTime
我想得到这样的结果:(我将尝试描述..)
Area, SubArea, SUM(StopNumber appened in january) as GEN,
SUM(StopNumber appened in february) as FEB, etc..
FROM Stops
GROUP BY Area, SubArea
有人能帮帮我吗? 谢谢你:))
答案 0 :(得分:1)
如果您喜欢我在评论中建议您的方法,您可以尝试这样的事情:
SELECT Area, SubArea, MONTH(Date) AS Month, SUM(StopNumber) AS NumStops
FROM Stops
GROUP BY Area, SubArea, MONTH(Date)
答案 1 :(得分:0)
这应该有效:
SELECT Area, SubArea,
SUM(CASE Month(Date) WHEN 1 THEN 1 ELSE 0 END) AS Jan,
SUM(CASE Month(Date) WHEN 2 THEN 1 ELSE 0 END) AS Feb,
SUM(CASE Month(Date) WHEN 3 THEN 1 ELSE 0 END) AS Mar,
SUM(CASE Month(Date) WHEN 4 THEN 1 ELSE 0 END) AS Apr,
SUM(CASE Month(Date) WHEN 5 THEN 1 ELSE 0 END) AS May,
SUM(CASE Month(Date) WHEN 6 THEN 1 ELSE 0 END) AS Jun,
SUM(CASE Month(Date) WHEN 7 THEN 1 ELSE 0 END) AS Jul,
SUM(CASE Month(Date) WHEN 8 THEN 1 ELSE 0 END) AS Aug,
SUM(CASE Month(Date) WHEN 9 THEN 1 ELSE 0 END) AS Sep,
SUM(CASE Month(Date) WHEN 10 THEN 1 ELSE 0 END) AS Oct,
SUM(CASE Month(Date) WHEN 11 THEN 1 ELSE 0 END) AS Nov,
SUM(CASE Month(Date) WHEN 12 THEN 1 ELSE 0 END) AS [Dec]
FROM Stops
GROUP BY Area, SubArea