0

我有一个困难的查询要做=)

Stops表具有以下列

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

有人可以帮我吗?谢谢你 :)

4

2 回答 2

1

如果您喜欢我在评论中建议您的方法,您可以尝试以下方法:

SELECT Area, SubArea, MONTH(Date) AS Month, SUM(StopNumber) AS NumStops
    FROM Stops
    GROUP BY Area, SubArea, MONTH(Date)
于 2013-07-12T08:48:28.227 回答
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
于 2013-07-12T08:45:21.290 回答