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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:52:24+00:00 2026-05-16T05:52:24+00:00

I have a table T1, it contains a NAME value (not unique), and a

  • 0

I have a table T1, it contains a NAME value (not unique), and a date range (D1 and D2 which are dates)
When NAME are the same, we make a union of the date ranges (e.g. B).

But as a result (X), we need to make intersection of all the date ranges

Edit:
Table T1

NAME | D1       | D2
A    | 20100101 | 20101211
B    | 20100120 | 20100415
B    | 20100510 | 20101230
C    | 20100313 | 20100610

Result :

X    | 20100313 | 20100415
X    | 20100510 | 20100610

Visually, this will give the following :

NAME        : date range
A           : [-----------------------]-----
B           : --[----]----------------------
B           : ----------[---------------]---
C           : -----[--------]---------------

Result :

X           : -----[-]----------------------
X           : ----------[---]---------------

Any idea how to get that using SQL / PL SQL ?

  • 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-16T05:52:25+00:00Added an answer on May 16, 2026 at 5:52 am

    here is a quick solution (may not be the most efficient):

    SQL> CREATE TABLE myData AS
      2  SELECT 'A' name, date'2010-01-01' d1, date'2010-12-11' d2 FROM DUAL
      3  UNION ALL SELECT 'B', date'2010-01-20', date'2010-04-15' FROM DUAL
      4  UNION ALL SELECT 'B', date'2010-05-10', date'2010-12-30' FROM DUAL
      5  UNION ALL SELECT 'C', date'2010-03-13', date'2010-06-10' FROM DUAL;
    
    Table created
    
    SQL> WITH segments AS (
      2  SELECT dat seg_low, lead(dat) over(ORDER BY dat) seg_high
      3    FROM (SELECT d1 dat FROM myData
      4           UNION
      5           SELECT d2 dat FROM myData)
      6  )
      7  SELECT s.seg_low, s.seg_high
      8    FROM segments s
      9    JOIN myData m ON s.seg_high > m.d1
     10                 AND s.seg_low < m.d2
     11   GROUP BY s.seg_low, s.seg_high
     12  HAVING COUNT(DISTINCT NAME) = 3;
    
    SEG_LOW     SEG_HIGH
    ----------- -----------
    13/03/2010  15/04/2010
    10/05/2010  10/06/2010
    

    I build all the possible successive date ranges and join this “calendar” with the sample data. This will list all ranges that have 3 values. You may need to merge the result if you add rows:

    SQL> insert into mydata values ('B',date'2010-04-15',date'2010-04-16');
    
    1 row inserted
    
    SQL> WITH segments AS (
      2  SELECT dat seg_low, lead(dat) over(ORDER BY dat) seg_high
      3    FROM (SELECT d1 dat FROM myData
      4           UNION
      5           SELECT d2 dat FROM myData)
      6  )
      7  SELECT MIN(seg_low), MAX(seg_high)
      8    FROM (SELECT seg_low, seg_high, SUM(gap) over(ORDER BY seg_low) grp
      9             FROM (SELECT s.seg_low, s.seg_high,
     10                           CASE
     11                              WHEN s.seg_low
     12                                   = lag(s.seg_high) over(ORDER BY s.seg_low)
     13                              THEN 0
     14                              ELSE 1
     15                           END gap
     16                      FROM segments s
     17                      JOIN myData m ON s.seg_high > m.d1
     18                                   AND s.seg_low < m.d2
     19                     GROUP BY s.seg_low, s.seg_high
     20                    HAVING COUNT(DISTINCT NAME) = 3))
     21   GROUP BY grp;
    
    MIN(SEG_LOW) MAX(SEG_HIGH)
    ------------ -------------
    13/03/2010   16/04/2010
    10/05/2010   10/06/2010
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table which contains due dates for individual member records. Each row
I have a (mysql) database table with the following columns: NAME | String (Unique)
If I have a table where the cells in a column should not have
I want to have a unique constraint on a column which I am going
I have a relational database with about 100 tables. Each table has unique, numerical,
I have a DataTable which I want to check if values in three of
I have a table of people quite standard stuff e.g. : CREATE TABLE [Contact](
I have a table 'TYPE' in this table i have 2 columns as 'SKEY'
I have this Database, not of my design but I have to work with
So I have been using data annotations for my validation in an MVC project

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.