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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:26:48+00:00 2026-06-15T10:26:48+00:00

Given the following set of data, I’m trying to determine how I can select

  • 0

Given the following set of data, I’m trying to determine how I can select the start and end dates of the combined date ranges, when they intersect with each other.

For instance, for PartNum 115678, I would want my final result set to display the date ranges 2012/01/01 - 2012/01/19 (rows 1, 2 and 4 combined since the date ranges intersect) and 2012/02/01 - 2012/03/28 (row 3 since this ones does not intersect with the range found previously).

For PartNum 213275, I would want to select the only row for that part, 2012/12/01 - 2013/01/01.

example-data

Edit:
I’m currently playing around with the following SQL statement, but it’s not giving me exactly what I need.

with DistinctRanges as (
    select distinct
        ha1.PartNum "PartNum",
        ha1.StartDt "StartDt",
        ha2.EndDt "EndDt"
    from dbo.HoldsAll ha1
    inner join dbo.HoldsAll ha2 
        on ha1.PartNum = ha2.PartNum
    where 
        ha1.StartDt <= ha2.EndDt 
        and ha2.StartDt <= ha1.EndDt
)
select 
    PartNum, 
    StartDt, 
    EndDt
from DistinctRanges

Here are the results of the query shown in the edit:

results from inline query

  • 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-15T10:26:49+00:00Added an answer on June 15, 2026 at 10:26 am

    You’re better off having a persisted Calendar table, but if you don’t, the CTE below will create it ad-hoc. The TOP(36000) part is enough to give you 10 years worth of dates from the pivot ('20100101') on the same line.

    SQL Fiddle

    MS SQL Server 2008 Schema Setup:

    create table data (
        partnum int,
        startdt datetime,
        enddt datetime,
        age int
    );
    insert data select 
    12345, '20120101', '20120116', 15 union all select
    12345, '20120115', '20120116', 1 union all select
    12345, '20120201', '20120328', 56 union all select
    12345, '20120113', '20120119', 6 union all select
    88872, '20120201', '20130113', 43;
    

    Query 1:

    with Calendar(thedate) as (
        select TOP(36600) dateadd(d,row_number() over (order by 1/0),'20100101')
          from sys.columns a
    cross join sys.columns b
    cross join sys.columns c
    ), tmp as (
       select partnum, thedate,
              grouper = datediff(d, dense_rank() over (partition by partnum order by thedate), thedate)
         from Calendar c
         join data d on d.startdt <= c.thedate and c.thedate <= d.enddt
    )
    select partnum, min(thedate) startdt, max(thedate) enddt
      from tmp
     group by partnum, grouper
     order by partnum, startdt
    

    Results:

    | PARTNUM |                         STARTDT |                          ENDDT |
    ------------------------------------------------------------------------------
    |   12345 |  January, 01 2012 00:00:00+0000 | January, 19 2012 00:00:00+0000 |
    |   12345 | February, 01 2012 00:00:00+0000 |   March, 28 2012 00:00:00+0000 |
    |   88872 | February, 01 2012 00:00:00+0000 | January, 13 2013 00:00:00+0000 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys, given a data set in plain text such as the following: ==Events==
Given the following simple data set, what is the best way to average the
Given the following DB schema and Sequel Models, how do I select a set
Given a data set with a time stamp and several columns of data following
Given the Following Code how Would i Change/Set my Silverlight WCF Service URI in
What should 'foo' be called, given the following? x.items is a set, y.values is
I'm trying to set up a Backbone application. This code below gives to following
// given following array: $data = array( 0=>array( data=>object1, col=>array( 0=>array( data=>object2, col=>array( 0=>array(
Given following Statment: String query = "Select * from T_spareParts where SparePartPK IN (?
Given the following data table definitions in SQLite Version 3.7.5: CREATE TABLE [Books] (

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.