我有一张桌子,上面有日期和种类。看起来像:
Date | Issue
2018-01-01 09:32:33.000 | Problem 1
2018-01-01 19:47:11.000 | Problem 1
2018-01-01 14:25:16.000 | Problem 2
2018-01-02 11:07:20.000 | Problem 1
2018-01-02 19:18:51.000 | Problem 2
2018-01-03 20:45:41.000 | Problem 1
2018-01-03 04:27:56.000 | Problem 1
2018-01-03 15:27:56.000 | Problem 2
2018-01-03 09:27:56.000 | Problem 2
2018-01-03 22:27:56.000 | Problem 3
我想计算每天每个问题的总数。我希望它看起来像:
Date | Issue | Amount
2018-01-01 | Problem 1 | 2
2018-01-01 | Problem 2 | 1
2018-01-02 | Problem 1 | 1
2018-01-02 | Problem 2 | 1
2018-01-03 | Problem 1 | 2
2018-01-03 | Problem 2 | 2
2018-01-03 | Problem 3 | 1
现在我有了这个,但显然不起作用:
SELECT CONVERT(VARCHAR, CONVERT(DATE TIME, Date), 23) AS Date,
Issue,
COUNT(Date) AS Amount
FROM Issues
GROUP BY Date, Issue
ORDER BY Date, Issue
谢谢。
答案 0 :(得分:2)
您将要关闭:
SELECT CONVERT(date,[Date]) AS [Date], --I suggest a better name for your column, as that's confusing
Issue,
COUNT(Date) AS Amount
FROM Issues
GROUP BY CONVERT(date,[Date]), Issue
ORDER BY [Date], Issue; --[Date] here will reference the alias, rather than the column
您还需要在GROUP BY
中使用表达式。