Skip to content
Advertisement

Oracle sql query to group two sets of dates if sequencials

I have the next table:

EMPLOYEE CODE START_DATE END_DATE
02097368 F7H3 09/07/2018 20/10/2018
02097368 F7H3 21/10/2018 05/01/2019
02097368 F7H3 06/01/2019 12/01/2019
02097368 F7H3 13/01/2019 02/02/2019
02097368 F7H3 03/02/2019 13/04/2019
02097368 F7S3 14/04/2019 04/01/2020
02097368 F7S3 05/01/2020 24/03/2020
02097368 F7S3 31/01/2021

I would like to group the data by employee, code and sequencial date(end_date to the next start_date if its 1 day later)

DESIRED RESULT:

EMPLOYEE CODE START_DATE END_DATE
02097368 F7H3 09/07/2018 13/04/2019
02097368 F7S3 14/04/2019 24/03/2020
02097368 F7S3 31/01/2021

Im trying this but im not getting the desired result

ACTUAL RESULT:

EMPLOYEE CODE START_DATE END_DATE
02097368 F7H3 09/07/2018 13/04/2019
02097368 F7S3 14/04/2019

Advertisement

Answer

I solved this with the following query though I know it’s not the most elegant one. Do replace TABLE_1 for the actual table name or view that holds your data

When running the following, I retrieved the below results

Results:

enter image description here

Results in text form that I don’t know how to format as a table:

EMPLOYEE CODE MIN(START_DATE) MAX(END_DATE) 02097368 F7H3 09-JUL-18 13-APR-19 02097368 F7S3 14-APR-19 24-MAR-20 02097368 F7S3 31-JAN-21 –

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement