SQL如何计算每天相同事件的数量
我有一张有日期和问题的桌子。它看起来像:SQL如何计算每天相同事件的数量,sql,sql-server,tsql,Sql,Sql Server,Tsql,我有一张有日期和问题的桌子。它看起来像: 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:
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
提前感谢。你很接近了:
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
您还需要
组中的表达式。非常感谢!现在效果很好。