我在SQL 2012中有两个表,一个包含事件,另一个包含请求 我希望能够计算出事件和请求的数量并按日分组,但似乎无法确定如何执行此操作。
示例数据如下
Incidents
ID Created Date
IR1 01/02/16
IR2 01/02/16
IR3 02/02/16
Requests
ID Created Date
SR4 01/02/16
SR5 02/02/16
SR6 03/02/16
我想看看
Incidents Requests Date
2 1 01/02/16
1 1 02/02/16
0 1 03/02/16
答案 0 :(得分:1)
使用UNION合并它们然后您可以分组并计算:
SELECT [Created Date], SUM(i) Incidents, SUM(r) Requests
FROM (
SELECT [Created Date], 1 i, 0 r FROM Incidents
UNION
SELECT [Created Date], 0, 1 FROM Requests
) tbl
GROUP BY [Created Date]