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

  • Home
  • SEARCH
  • 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 946503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:55:40+00:00 2026-05-15T22:55:40+00:00

A table exists in Microsoft SQL Server with record ID, Start Date, End Date

  • 0

A table exists in Microsoft SQL Server with record ID, Start Date, End Date and Quantity.

The idea is that for each record, the quantity/total days in range = daily quantity.

Given that a table containing all possible dates exists, how can I generate a result set in SQL Server to look like the following example?

EX:

RecordID | Start Date | End Date  | Quantity
1        |  1/1/2010  | 1/5/2010  | 30000
2        |  1/3/2010  | 1/9/2010  | 20000
3        |  1/1/2010  | 1/7/2010  | 10000

Results as
1        | 1/1/2010 |  QTY (I can do the math easy, just need the dates view)
1        | 1/2/2010 | 
1        | 1/3/2010 | 
1        | 1/4/2010 | 
1        | 1/3/2010 | 
2        | 1/4/2010 | 
2        | 1/5/2010 | 
2        | 1/6/2010 | 
2        | 1/7/2010 | 
2        | 1/8/2010 | 
2        | 1/9/2010 | 
3        | 1/1/2010 | 
3        | 1/2/2010 | 
3        | 1/3/2010 | 
3        | 1/4/2010 | 
3        | 1/5/2010 | 
3        | 1/6/2010 | 
3        | 1/7/2010 | 

Grouping on dates I could get then get the sum of quantity on that day however the final result set can’t be aggregate due to user provided filters that may exclude some of these records down the road.

EDIT

To clarify, this is just a sample. The filters are irrelevant as I can join to the side to pull in details related to the record ID in the results.

The real data contains N records which increases weekly, the dates are never the same. There could be 2000 records with different start and end dates… That is what I want to generate a view for. I can right join onto the data to do the rest of what I need

I should also mention this is for past, present and future data. I would love to get rid of a temporary table of dates. I was using a recursive query to get all dates that exist within a 50 year span but this exceeds MAXRECURSION limits for a view, that I cannot use.

  • 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-05-15T22:55:40+00:00Added an answer on May 15, 2026 at 10:55 pm

    Answer

    select RecordId,d.[Date], Qty/ COUNT(*) OVER (PARTITION BY RecordId) AS Qty
    from EX join Dates d on d.Date between [Start Date] and [End Date]
    ORDER BY RecordId,[Date]
    

    NB: The below demo CTEs use the date datatype which is SQL Server 2008 the general approach should work for SQL2005 as well though.

    Test Case

    /*CTEs for testing purposes only*/
    
    WITH EX AS
    (
        SELECT 1 AS RecordId, 
        cast('1/1/2010' as date) as [Start Date], 
        cast('1/5/2010' as date) as  [End Date], 
        30000 AS Qty
    union all
        SELECT 2 AS RecordId, 
        cast('1/3/2010' as date) as [Start Date], 
        cast('1/9/2010' as date) as  [End Date], 
        20000  AS Qty
    ),Dates AS /*Dates Table now adjusted to do greater range*/
    (
    
    SELECT  DATEADD(day,s1.number + 2048*s2.number,'1990-01-01') AS [Date] 
    FROM master.dbo.spt_values s1 CROSS JOIN master.dbo.spt_values s2
    where s1.type='P' AND s2.type='P' and s2.number <= 8
    order by  [Date] 
    )
    
    
    select RecordId,d.[Date], Qty/ COUNT(*) OVER (PARTITION BY RecordId) AS Qty
    from EX join Dates d on d.Date between [Start Date] and [End Date]
    ORDER BY RecordId,[Date]
    

    Results

    RecordId    Date       Qty
    ----------- ---------- -----------
    1           2010-01-01 6000
    1           2010-01-02 6000
    1           2010-01-03 6000
    1           2010-01-04 6000
    1           2010-01-05 6000
    2           2010-01-03 2857
    2           2010-01-04 2857
    2           2010-01-05 2857
    2           2010-01-06 2857
    2           2010-01-07 2857
    2           2010-01-08 2857
    2           2010-01-09 2857
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to check if the global Temporary table exists in SQL server, if yes
How would you write the following in Microsoft SQL Server 2008? IF EXISTS(SELECT *
We have a CallLog table in Microsoft SQL Server 2000 . The table contains
Microsoft SQL Server seems to check column name validity, but not table name validity
I have a database (using Microsoft SQL Server Management Studio Express) that is currently
Using Microsoft SQL Server 2008. I have a table of interest rates. I've manually
I have a table with data from an SQL database and the table exists
I'm attempting to recover the data from a specific table that exists in a
Microsoft SQL Server 2008 (SP1), getting an unexpected 'Conversion failed' error. Not quite sure
Is there a tool out there that can analyse SQL Server databases for potential

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.