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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:41:16+00:00 2026-05-18T12:41:16+00:00

I want to display data based on start date and end date. a code

  • 0

I want to display data based on start date and end date. a code can contain different dates. if any time intervel is continues then I need to merge that rows and display as single row
Here is sample data

Code  Start_Date   End_Date     Volume
470   24-Oct-10    30-Oct-10    28
470   17-Oct-10    23-Oct-10     2
470   26-Sep-10     2-Oct-10     2
471   22-Aug-10    29-Aug-10     2
471   15-Aug-10    21-Aug-10     2

The output result I want is

Code  Start_Date   End_Date     Volume
470   17-Oct-10    30-Oct-10    30
470   26-Sep-10     2-Oct-10     2
471   15-Aug-10    29-Aug-10     4

a code can have any no. of time intervels. Pls help. Thank you

  • 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-18T12:41:16+00:00Added an answer on May 18, 2026 at 12:41 pm

    Based on your sample data (which I’ve put in a table called Test), and assuming no overlaps:

    ;with Ranges as (
        select Code,Start_Date,End_Date,Volume from Test
        union all
        select r.Code,r.Start_Date,t.End_Date,(r.Volume + t.Volume)
        from
            Ranges r
                inner join
            Test t
                on
                    r.Code = t.Code and
                    DATEDIFF(day,r.End_Date,t.Start_Date) = 1
    ), ExtendedRanges as (
    select Code,MIN(Start_Date) as Start_Date,End_Date,MAX(Volume) as Volume
    from Ranges
    group by Code,End_Date
    )
    select Code,Start_Date,MAX(End_Date),MAX(Volume)
    from ExtendedRanges
    group by Code,Start_Date
    

    Explanation:

    The Ranges CTE contains all rows from the original table (because some of them might be relevant) and all rows we can form by joining ranges together (both original ranges, and any intermediate ranges we construct – we’re doing recursion here).

    Then ExtendedRanges (poorly named) finds, for any particular End_Date, the earliest Start_Date that can reach it.

    Finally, we query this second CTE, to find, for any particular Start_Date, the latest End_Date that is associated with it.

    These two queries combine to basically filter the Ranges CTE down to “the widest possible Start_Date/End_Date pair” in each set of overlapping date ranges.

    Sample data setup:

    create table Test (
        Code int not null,
        Start_Date date not null,
        End_Date date not null,
        Volume int not null
    )
    insert into Test(Code,  Start_Date,   End_Date,     Volume)
    select 470,'24-Oct-10','30-Oct-10',28 union all
    select 470,'17-Oct-10','23-Oct-10',2 union all
    select 470,'26-Sep-10','2-Oct-10',2 union all
    select 471,'22-Aug-10','29-Aug-10',2 union all
    select 471,'15-Aug-10','21-Aug-10',2
    go
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to display tabular type data, but it will not be coming from
My data is such that I want to display values for the Employee and
I want to display the TIME field from my mysql table on my website,
I want to display dates in the format: short day of week, short month,
I'm working on an application that stores conferences with their start and end date.
I have a list view that can display items based on internal state (it
I want to display the current host and database names in a view. Where
I have a Database table that I want to display in a DataGridView. However,
i want to display some information in a listview using the GridView. i have
I want to display a thumbnail image in a cell of tableViewController , 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.