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

  • Home
  • SEARCH
  • 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 1087375
In Process

The Archive Base Latest Questions

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

I have a table with date ranges. I want to look for gaps between

  • 0

I have a table with date ranges. I want to look for gaps between these ranges.
I’ve figured out that I may left join table to itself and calculate the difference.

Current table:

date_begin    date_end
2010-08-01    2010-08-15
2010-08-16    2010-08-30
2010-08-31    2010-09-12
2010-10-01    2010-10-15

I want to get:

date_begin    date_end    -   date_begin2    DATEDIFF(date_begin2 - date_end)
                              2010-08-01       
2010-08-01    2010-08-15      2010-08-16       ...
2010-08-16    2010-08-30      2010-08-31       ...
2010-08-31    2010-09-12      2010-10-01       ...
2010-10-01    2010-10-15

I use the following query:

SELECT pay1.date_begin, pay1.date_end, 
       pay2.date_begin, pay2.date_end, DATEDIFF( pay2.date_begin, pay1.date_end) 
FROM `payment_employees` AS pay1
LEFT JOIN `payment_employees` AS pay2 ON pay2.date_begin > pay1.date_end
GROUP BY pay1.date_begin
ORDER BY pay1.date_begin ASC

But the result is

pay1.date_begin date_end    pay2.date_begin date_end              difference
2010-08-01    2010-08-15    2010-08-31    2010-09-12            16  wrong
2010-08-16    2010-08-30    2010-08-31    2010-09-12            1   correct
2010-08-31    2010-09-12    2010-10-01    2010-10-15            19  correct
2010-10-01    2010-10-15    NULL              NULL

If I delete the GROUP BY I can see that there are correct rows in the result. I just can’t figure out how to get them.

Is there a way to sort table before being joined?

  • 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-16T22:55:53+00:00Added an answer on May 16, 2026 at 10:55 pm

    If you want the rows in your tables sorted before being joined, you could replace either or both of the real tables in your join with derived tables like this…

    SELECT pay1.date_begin,
           pay1.date_end, 
           pay2.date_begin,
           pay2.date_end,
           DATEDIFF( pay2.date_begin, pay1.date_end) 
        FROM (SELECT *
                  FROM `payment_employees`
                  ORDER BY date_begin) AS pay1
              LEFT JOIN (SELECT *
                             FROM `payment_employees`
                             ORDER BY date_end) AS pay2
                 ON pay2.date_begin > pay1.date_end
        GROUP BY pay1.date_begin
        ORDER BY pay1.date_begin ASC
    

    I’m not sure if that’ll solve the substantive problem, though.

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

Sidebar

Related Questions

Hi all I have an appointments table that has among other fields a DATE
My question about My SQL is, I have a table where users enter time
please i have a modified_by column in mysql table with string like this 40,1280825613|40,1280825617
I have a bunch of XML files, along with the DTD, that each have
I have created a procedure in dynamic SQL which has a select statement and
I am trying to implement a lookup feature in Excel Vba. I do not
I am trying to design a data warehouse for a licensing vendor, who sells
I believe this has been a bug/problem in SQL 2000/2005 ... If my results
I am implementing two dropdown lists where Fund dropdown list is populated by the
I would appreciate some advice on how to structure a database for the following

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.