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 9175887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:59:25+00:00 2026-06-17T16:59:25+00:00

I have a database table containing time-periods and amounts. Think of them as contracts

  • 0

I have a database table containing time-periods and amounts. Think of them as contracts with a duration and a price per day:

start      | end        | amount_per_day
2013-01-01 | 2013-01-31 | 100
2013-02-01 | 2013-06-30 | 200
2013-01-01 | 2013-06-30 | 100
2013-05-01 | 2013-05-15 | 50
2013-05-16 | 2013-05-31 | 50

I would like to make a query that will display the totals for each period, i.e.:

From 2013-01-01 to 2013-01-31, the first and third contract are active, so the total amount per day is 200. From 2013-02-01 to 2013-04-30, the second and third row are active, so the total is 300. From 2013-05-01 to 2013-05-15 the second, third and fourth row are active, so the total is 350. From 2013-05-16 to 2013-05-31 the second, third and fifth row are active, so the total is again 350. Finally, from 2013-06-01 to 2013-06-30 only the second and third are active, so the total is back to 300.

start      | end        | total_amount_per_day
2013-01-01 | 2013-01-31 | 200
2013-02-01 | 2013-04-30 | 300
2013-05-01 | 2013-05-31 | 350
2013-06-01 | 2013-06-30 | 300

(It is not necessary to detect that the intervals 2013-05-01 -> 2013-05-15 and 2013-05-16 -> 2013-05-31 have the same totals and merge them, but it would be nice).

I would prefer a portable solution, but if it is not possible a SQL Server will work, too.

I can make small changes to the structure of the table, so if it would make the query simpler to e.g. notate the time-periods with the end-date exclusive (so the first period would be start = 2013-01-01, end = 2013-02-01) feel free to make such suggestions.

  • 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-17T16:59:26+00:00Added an answer on June 17, 2026 at 4:59 pm

    I’ll start with the full query and then break it down and explain it. This is SQL-Server specific, but with minor tweaks could be adapted to any DMBS that supports analytical functions.

    WITH Data AS
    (   SELECT  Start, [End], Amount_Per_Day
        FROM    (VALUES
                    ('20130101', '20130131', 100),
                    ('20130201', '20130630', 200),
                    ('20130101', '20130630', 100),
                    ('20130501', '20130515', 50),
                    ('20130516', '20130531', 50)
                ) t (Start, [End], Amount_Per_Day)
    ), Numbers AS
    (   SELECT  Number
        FROM    Master..spt_values
        WHERE   Type = 'P'
    ), DailyData AS
    (   SELECT  [Date] = DATEADD(DAY, Number, Start),
                [AmountPerDay] = SUM(Amount_Per_Day)
        FROM    Data
                INNER JOIN Numbers
                    ON Number BETWEEN 0 AND DATEDIFF(DAY, Start, [End])
        GROUP BY DATEADD(DAY, Number, Start)
    ), GroupedData AS
    (   SELECT  [Date],
                AmountPerDay,
                [GroupByValue] = DATEADD(DAY, -ROW_NUMBER() OVER(PARTITION BY AmountPerDay ORDER BY [Date]), [Date])
        FROM    DailyData
    )
    SELECT  [Start] = MIN([Date]),
            [End] = MAX([Date]),
            AmountPerDay
    FROM    GroupedData
    GROUP BY AmountPerDay, GroupByValue
    ORDER BY [Start], [End];
    

    The Data CTE is just your sample data.

    The Numbers CTE is just a sequence of numbers from 0 – 2047 (If your start and end dates are more than 2047 days apart this will fail and will need adapting slightly)

    The Next CTE DailyData simply uses the numbers to expand your ranges into their individual dates, so

    20130101, 20130131, 100
    

    Becomes

    20130101, 100
    20130102, 100
    20130103, 100
    ....
    20130131, 100
    

    Then it is just a case of grouping the data by the amount per day with the help of the ROW_NUMBER function to find when it changes and define ranges of similar amounts per day, then getting the MIN and MAX date for each range.

    I always struggle to explain/demonstrate the exact workings of this method of grouping ranges, if it doesn’t make sense it is perhaps easiest seen for your self if you just use SELECT * FROM DailyData at the end to see the raw unaggregated data

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

Sidebar

Related Questions

I have a database table containing dates (`date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00').
I have a database table containing the news on my site. It's divided into
In my database I have table AnimalDetails containing animalNumber(primary key),name,breed dateofbirth and an image
I have a java class containing all the columns of a database table as
I have a database containing a single huge table. At the moment a query
Suppose I have an Oracle 11.2 database containing the following table: TABLE: SURVEY PARAMETER
I have a database table containing credit card records. One of the fields is
I have an Access database table containing a row of dates in DateTime format.
I have a database table containing data for a submitted application form with a
I have a database containing a table named Couple which representing data couples. This

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.