Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Lead/Lag query

TarabasOct 9 2017 — edited Oct 9 2017

Hi all,

Trying to solve next issue with lead/lag functions:

Got table s09:

guidstart_dateend_date
xxx01.08.201609.09.2016
xxx10.09.201624.11.2016
xxx25.11.201631.12.9999
yyy01.08.201624.11.2016
yyy25.11.201611.12.2016
yyy12.12.201601.01.2017
yyy10.02.201722.04.2017
yyy23.04.201731.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:

guidstart_dateend_date
xxx01.08.201631.12.9999
yyy01.08.201601.01.2017
yyy10.02.201731.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!

This post has been answered by mathguy on Oct 9 2017
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 6 2017
Added on Oct 9 2017
5 comments
363 views