Skip to main content
Solved

Group by sequel date


Forum|alt.badge.img+5

Dears,

I want group by based on sequel of dates.

valid from: 1/1/2019 to valid_to: 12/18/2020 should be one record and it is sequel of dates.

and valid from: 3/1/2021 to valid_to: 3/31/2021 should be another record.

so the result will be two records.

 

how to partition based on sequel of dates.

Best answer by Tomas Ruderfelt

I do not exactly know which table you are using but here is an example.

Table data:

 

 

Select statement to find interval of intervals:

SELECT company_id, emp_no,date_of_employment,
       (SELECT MAX(date_of_leaving)
        FROM emp_employed_time_row t
        CONNECT BY t.company_id = PRIOR t.company_id
        AND t.emp_no = PRIOR t.emp_no
        AND t.date_of_employment = CASE WHEN PRIOR t.date_of_leaving = to_date('9999-12-31','YYYY-MM-DD') THEN PRIOR t.date_of_leaving ELSE PRIOR t.date_of_leaving + 1 END
        START WITH t.company_id = e.company_id
        AND t.emp_no = e.emp_no
        AND t.date_of_employment = e.date_of_employment) date_of_leaving
FROM emp_employed_time_row e
WHERE company_id = '300'
AND NOT EXISTS (SELECT 1
                FROM emp_employed_time_row x
                WHERE x.company_id = e.company_id
                AND x.emp_no = e.emp_no
                AND e.date_of_employment -1= x.date_of_leaving)

 

 

Short explanation:

The main select fetches all start of intervals you want. Then the end date is fetched by a query in the SELECT part.

I had to do special handling of date 9999-21-31 due to the data I had. Not needed if you do not have/will have that kind of data.

View original
This topic has been closed for comments

3 replies

EntNadeeL
Hero (Partner)
Forum|alt.badge.img+10
  • Hero (Partner)
  • 181 replies
  • April 6, 2021
Ramadass wrote:

Dears,

I want group by based on sequel of dates.

valid from: 1/1/2019 to valid_to: 12/18/2020 should be one record and it is sequel of dates.

and valid from: 3/1/2021 to valid_to: 3/31/2021 should be another record.

so the result will be two records.

 

how to partition based on sequel of dates.

Hi @Ramadass ,

When you group like that either you need to have condition for each date range or it should be like 

set of records per month. 

What are the conditions on your scenario? 

Above two date ranges have nothing in similar. 


Forum|alt.badge.img+5
  • Author
  • Sidekick (Partner)
  • 14 replies
  • April 6, 2021

Hi,

If you see first three rows of valid from and valid_to it is sequel, no gaps in dates but between third row valid_to and fourth row valid from there is gap in dates.

I want to show first three rows as one record if I take as min(valid_from) and max(valid_to) and fourth row as one record

 

Thanks in advance...


Forum|alt.badge.img+20
  • Superhero (Employee)
  • 492 replies
  • Answer
  • April 6, 2021

I do not exactly know which table you are using but here is an example.

Table data:

 

 

Select statement to find interval of intervals:

SELECT company_id, emp_no,date_of_employment,
       (SELECT MAX(date_of_leaving)
        FROM emp_employed_time_row t
        CONNECT BY t.company_id = PRIOR t.company_id
        AND t.emp_no = PRIOR t.emp_no
        AND t.date_of_employment = CASE WHEN PRIOR t.date_of_leaving = to_date('9999-12-31','YYYY-MM-DD') THEN PRIOR t.date_of_leaving ELSE PRIOR t.date_of_leaving + 1 END
        START WITH t.company_id = e.company_id
        AND t.emp_no = e.emp_no
        AND t.date_of_employment = e.date_of_employment) date_of_leaving
FROM emp_employed_time_row e
WHERE company_id = '300'
AND NOT EXISTS (SELECT 1
                FROM emp_employed_time_row x
                WHERE x.company_id = e.company_id
                AND x.emp_no = e.emp_no
                AND e.date_of_employment -1= x.date_of_leaving)

 

 

Short explanation:

The main select fetches all start of intervals you want. Then the end date is fetched by a query in the SELECT part.

I had to do special handling of date 9999-21-31 due to the data I had. Not needed if you do not have/will have that kind of data.


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings