我的SQL查询有些问题。 我选择一些具有特定日期范围的值。 例如2018-05-01到2018-05-13这是输出。
SUM CalendarWeek
8 18
5 19
如果用户现在将选择2018-04-01和2018-05-13之间的日期,我希望在没有值时显示0。
例如:
SUM CalendarWeek
0 13
0 14
0 15
0 16
0 17
8 18
5 19
我的查询:
SELECT SUM(Codes) AS 'Sum', CW FROM(
SELECT Count(*) AS 'Codes', DATEPART(wk, ScanDate) AS 'CW',
FROM [Table]
WHERE CONVERT(date, ScanDate, 102) >= '2018-01-01' AND CONVERT(date, ScanDate, 102) <= '2018-05-13'
GROUP BY ScanDate, DATEPART(wk, ScanDate)
UNION ALL
SELECT Count(*) AS 'Codes', DATEPART(wk, ScanDate) AS 'CW', ScanDate
FROM [Table_Archive]
WHERE CONVERT(date, ScanDate, 102) >= '2018-01-01' AND CONVERT(date, ScanDate, 102) <= '2018-05-13'
GROUP BY ScanDate, DATEPART(wk, ScanDate)) test
GROUP BY CW, ScanDate
ORDER BY CW ASC
任何想法如何解决这个问题? 感谢
答案 0 :(得分:0)
SELECT ISNULL(SUM, 0), CalendarWeek