Hi all,
Trying to solve next issue with lead/lag functions:
Got table s09:
| guid | start_date | end_date |
|---|
| xxx | 01.08.2016 | 09.09.2016 |
| xxx | 10.09.2016 | 24.11.2016 |
| xxx | 25.11.2016 | 31.12.9999 |
| yyy | 01.08.2016 | 24.11.2016 |
| yyy | 25.11.2016 | 11.12.2016 |
| yyy | 12.12.2016 | 01.01.2017 |
| yyy | 10.02.2017 | 22.04.2017 |
| yyy | 23.04.2017 | 31.12.9999 |
I need to group ordered by start_date rows by guid into one group if start_date-1 of current row equals to end_date of previows row.
The result I need:
| guid | start_date | end_date |
|---|
| xxx | 01.08.2016 | 31.12.9999 |
| yyy | 01.08.2016 | 01.01.2017 |
| yyy | 10.02.2017 | 31.12.9999 |
I try to solve this with lead/lag functions:
select case
when lag(w1.end_date, 1, w1.end_date) over (order by w1.guid, w1.start_date)=w1.start_date-1
then lag(w1.start_date,1, w1.start_date) over (order by w1.guid, w1.start_date)
else w1.start_date
and first_start_date,
w1.end_date
from s09
This works only for first two rows..
Third row will get start_date of 2nd row.
What functoin can hold start_date of first row in the group for all rows, that satisfy condition: start_date-1 of currentt row=end_date of prev row of the group?
Any suggestions what function will help?
Thanks for any answers!