0

我有一张看起来像的桌子

    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

请问我该怎么做?

4

2 回答 2

0

像这样的东西:

SQLFIDDLE示例

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) |
于 2012-12-04T11:03:22.990 回答
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 的任何行,看起来就像您所追求的

于 2012-12-04T11:12:45.127 回答