Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8955915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:39:52+00:00 2026-06-15T14:39:52+00:00

I have a table with that looks like Date | BookID | Rating |

  • 0

I have a table with that looks like

    Date    |  BookID   |  Rating  |  Review
2012-10-12  |    2      |    3     |    3
2012-10-13  |    2      |    7     |    9
2012-10-16  |    3      |    4     |    2

Each day, a book can receive a number of rating and review
I am trying to write a query that given two dates, it displays the rating and review for each book, for EACH day in between the two given dates. Even when that day has no rating or no review I still want to display the date and the BookID
For example, in the table above, I want the query to return something like

 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

How can I do this please?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:39:53+00:00Added an answer on June 15, 2026 at 2:39 pm
    --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)
    

    This gives the results:

    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
    

    That’s one extra row than the results you posted, but I believe is correct – it doesn’t return any rows for a bookid before the first review which looks like what you were after

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that looks something like this Employee Date Sean 21-07-2012 Peter
So I have a table that looks like this: ProjectID TaskID Date 000 001
I have a table in MySQL That looks like the following: date |storenum |views
Greetings, I have a table that looks like the following: date value 2007-11-05 134
I have a table that looks like this: (so you can picture it visually)
I have a table with weekly data that looks like: userID Site date ------
I have a table that looks like this: UID DATE GROUP VALUE The data
Hi I have a mysql table that looks like so: date time 2009-07-31 02:30
I have a table that looks like this: pk client value date 1 001
I have a table that looks something like this: ________________________ |id|value|date|approved| ----------------------- What I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.