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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:29:46+00:00 2026-06-01T07:29:46+00:00

Im trying to write a query which will emit : the date/s which Overlapped

  • 0

Im trying to write a query which will emit :

the date/s which Overlapped at most.

the format is d/m/yyyy

so here i have date ranges :

dateStart—–dateEnd

  1/1---7/1                          

         8/1--15/1                   

               16/1------20/1               

         8/1--------------21/1       

                17/1---19/1 

                 18/1--19/1  

this is the desired result analyze :

enter image description here

the 2 common days at the left are 8/1 and 9/1 (appears at 2 ranges)

the 4 common days at the right are 18/1 and 19/1 (appears at 4 ranges… and 4>2 so it should win.)

desired result :

18/1

19/1

they both appears the most overlapped.

edit

this is the script of the datetimes ranges.

DECLARE @t table( dt1 DATETIME , dt2 DATETIME)

INSERT INTO @t
SELECT '20110101','20110107'
UNION ALL
SELECT '20110108','20110115'
UNION ALL
SELECT '20110116','20110120'
UNION ALL
SELECT '20110108','20110121'
UNION ALL
SELECT '20110117','20110119'
UNION ALL
SELECT '20110118','20110119'
  • 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-01T07:29:48+00:00Added an answer on June 1, 2026 at 7:29 am

    I’m afraid that this is not exactly what you’re looking for, but maybe it helps you anyway(i’m running out of time):

    DECLARE @tbl table( startdate DATETIME , enddate DATETIME)
    
    INSERT INTO @tbl
    SELECT '20110101','20110107'
    UNION ALL
    SELECT '20110108','20110115'
    UNION ALL
    SELECT '20110116','20110120'
    UNION ALL
    SELECT '20110108','20110121'
    UNION ALL
    SELECT '20110117','20110119'
    UNION ALL
    SELECT '20110118','20110119'
    
    ;with overlapping_events as(
        select startdate, enddate
           , (select sum(inTimeSpan) 
        from (
           select case when startdate<=events.startdate then 1 else 0 end
             + case when enddate <= events.startdate then -1 else 0 end as inTimeSpan
           from @tbl 
           where startdate <= events.startdate
             or enddate <= events.startdate) as previous
        ) as overlapping
        from @tbl events
    )
    select oe.* 
    from overlapping_events oe 
    order by overlapping desc, startdate asc, enddate asc
    
    startdate                     enddate                     overlapping
    2011-01-18 00:00:00.000   2011-01-19 00:00:00.000         4
    2011-01-17 00:00:00.000   2011-01-19 00:00:00.000         3
    2011-01-08 00:00:00.000   2011-01-15 00:00:00.000         2
    2011-01-08 00:00:00.000   2011-01-21 00:00:00.000         2
    2011-01-16 00:00:00.000   2011-01-20 00:00:00.000         2
    2011-01-01 00:00:00.000   2011-01-07 00:00:00.000         1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a query that will return data grouped by date ranges
I'm pretty new to MYSQL. I'm trying to write a query which will search
I'm trying to write an HQL query which will calculate an average rating for
I'm trying to write a single query which will include one of the two
I'm trying to write a query to tell me which orders have valid promocodes.
I am trying to write a LINQ query which will return true if there
I am trying to write an SQL (Server) query which will return all events
I have been trying to write a query for a public transportation system which
I'm trying to write a query which will get the maximum scores for the
I am trying to write a query that will return only the most recent

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.