我正在使用如下查询从数据库中提取记录:
SELECT date as "Date", count(date) as "NumIssues"
FROM Issues
WHERE Date BETWEEN '2015-03-25' AND '2015-03-28'
GROUP BY date
ORDER BY date;
查询工作正常,但即使没有指定日期的值,我也需要它来获取结果,并为NumIssues值返回0。
最好的解决方法是提出案例陈述吗?提前谢谢!
答案 0 :(得分:1)
;WITH dates ( "Date" ) AS (
SELECT CONVERT( DATE, '2015-03-25' )
UNION ALL
SELECT DATEADD( DAY, 1, Date )
FROM dates
WHERE DATEADD( DAY, 1, Date ) < '2015-03-28'
)
SELECT d.Date
, COUNT(date) as "NumIssues"
FROM dates AS d
LEFT JOIN Issues AS i
ON i.date = d.Date
GROUP BY d.date
ORDER BY d.date;