我对我的报告有疑问,报告格式如下
**Date** **Received** **Closed** **Pending**
12/01/10 1000 900 100
12/02/10 2000 1000 1000
12/03/10 1500 1300 200
上述报告显示帮助台门票收到,已关闭,待定计数为日期。
如何创建动态SQL查询以显示上述结果?
请提供我的想法或一些示例SQL查询。
答案 0 :(得分:2)
分别编写已接收,已关闭和待处理的查询,然后将它们连接在一起,如下所示:
SELECT r.[Date], r.Count As Received, c.Count As Closed, p.Count AS Pending
FROM
( /* Received query here */ ) r
FULL JOIN
( /* Closed query here */) c ON c.[Date] = r.[Date]
FULL JOIN
( /* Pending query here */) p ON p.[Date] = r.[Date]
我选择了一个完整的连接,因为你不希望任何一点上的零结果强制从结果中剔除一行。
答案 1 :(得分:2)
使用:
SELECT CONVERT(VARCHAR, t.submitteddate, 101) AS [date].
COUNT(t.submitteddate) AS received
SUM(CASE WHEN t.status = 'closed' THEN 1 ELSE 0 END) AS closed,
SUM(CASE WHEN t.status = 'pending' THEN 1 ELSE 0 END) AS pending
FROM YOUR_TABLE t
GROUP BY CONVERT(VARCHAR, t.submitteddate, 101)
ORDER BY [date]
如果您想查看没有销售的日期,您将需要派生一个日期表,然后将上面的查询左键加入到基于日期的查询。