我的表格看起来像
Date | BookID | Rating | Review
2012-10-12 | 2 | 3 | 3
2012-10-13 | 2 | 7 | 9
2012-10-16 | 3 | 4 | 2
每天,一本书都可以获得一些评级和评论 我正在尝试编写一个给出两个日期的查询,它显示每本书的评级和评论,在两个给定日期之间的每一天。即使当天没有评级或没有评论,我仍然希望显示日期和BookID 例如,在上表中,我希望查询返回类似
的内容 Date | BookID | Rating | Review
2012-10-12 | 2 | 3 | 3
2012-10-13 | 2 | 7 | 9
2012-10-14 | 2 | null | null
2012-10-15 | 2 | null | null
2012-10-16 | 3 | 4 | 2
我该怎么办呢?
答案 0 :(得分:0)
这样的事情:
<强> SQLFIDDLEEXAMPLE 强>
WITH mycte AS
(
SELECT cast('2012-01-01' AS datetime) DateValue
UNION ALL
SELECT DateValue + 1
FROM mycte
WHERE DateValue + 1 < '2014-12-31'
)
SELECT
CONVERT(char(10), mycte.DateValue,126) as Date,
b.BookID,
(SELECT Rating FROM tbl WHERE BookID = b.BookID
AND Date = mycte.DateValue) as Rating,
(SELECT Review FROM tbl WHERE BookID = b.BookID
AND Date = mycte.DateValue) as Review
FROM mycte, (SELECT distinct BookID
FROM tbl) b
WHERE mycte.DateValue >= '2012-10-12'
AND mycte.DateValue <= '2012-10-15'
OPTION (MAXRECURSION 0)
结果:
| DATE | BOOKID | RATING | REVIEW |
-----------------------------------------
| 2012-10-12 | 2 | 3 | 3 |
| 2012-10-12 | 3 | (null) | (null) |
| 2012-10-13 | 2 | 7 | 9 |
| 2012-10-13 | 3 | (null) | (null) |
| 2012-10-14 | 2 | (null) | (null) |
| 2012-10-14 | 3 | (null) | (null) |
| 2012-10-15 | 2 | (null) | (null) |
| 2012-10-15 | 3 | (null) | (null) |
答案 1 :(得分:0)
--set up test data
DECLARE @Reviews TABLE([Date] date, BookId int, Rating int, Review int)
INSERT INTO @Reviews VALUES
('2012-10-12', 2, 3, 3),
('2012-10-13', 2, 7, 9),
('2012-10-16', 3, 4, 2)
--setup query parameters
DECLARE @StartDate date,
@EndDate date
SELECT @StartDate = '2012-10-12',
@EndDate = '2012-10-16'
--run query
;WITH IntervalSet AS
(
SELECT @StartDate AS [Date]
UNION ALL
SELECT DATEADD(DAY,1,[Date]) AS [Date]
FROM IntervalSet
WHERE DATEADD(DAY,1,[Date])<=@EndDate
)
SELECT I.[Date], B.BookId, R.Rating, R.Review
FROM
(
SELECT BookId, Min([Date]) as EarliestDate
FROM @Reviews
GROUP BY BookId
) B
JOIN IntervalSet I
ON I.[Date] >= b.EarliestDate
LEFT
JOIN @Reviews R
ON R.[Date] = I.[Date]
AND R.BookId = B.BookId
ORDER BY I.[Date], B.BookId
OPTION(MAXRECURSION 0)
这给出了结果:
Date BookId Rating Review
---------- ----------- ----------- -----------
2012-10-12 2 3 3
2012-10-13 2 7 9
2012-10-14 2 NULL NULL
2012-10-15 2 NULL NULL
2012-10-16 2 NULL NULL
2012-10-16 3 4 2
这比您发布的结果多了一行,但我认为是正确的 - 在第一次审核之前它没有为bookid返回任何行,这看起来像你之后的那样