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!

how to use where clause on LAG function result

3354727Nov 29 2016 — edited Dec 2 2016

Hi,

I am using LAG function to compare current row data versus previous row data , start_dt/ end_dt.

in below SQL inner query returning results correctly, but when I use outer where condition (where start_dt<prev_end_dt) considering all data and returning results,

in there any way I can use poli_id&iss_cd columns to get correct results. please advise.

LeadTestData.PNG

SQL statements are posted as text. create table pol_tbl (pol_sk integer,pol_id integer, iss_cd varchar(10), start_dt date, end_dt date); insert into pol_tbl values(1,821,'BE','01-JAN-2013','31-dec-2013'); insert into pol_tbl values(2,821,'BE','01-JAN-2014','26-DEC-2015'); insert into pol_tbl values(3,821,'BE','23-Dec-2013',''); insert into pol_tbl values(4,821,'OP','01-JAN-2013','13-JUL-2015'); insert into pol_tbl values(5,821,'OP','14-JUL-2015',''); insert into pol_tbl values(6,821,'PU','01-JAN-2014','15-JUL-2014'); insert into pol_tbl values(7,821,'PU','14-JUL-2015',''); From above data query need to pull record where pol_sk=2 (with data combination pol_id and iss_cd ), because end_dt of this record is grater than start_dt of pol_sk=3 (with data combination pol_id and iss_cd ) in same way query need to pull record where pol_sk=6 (with data combination pol_id and iss_cd ), because end_dt of this record is grater than start_dt of pol_sk=7 (with data combination pol_id and iss_cd )

This post has been answered by Frank Kulash on Dec 1 2016
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 30 2016
Added on Nov 29 2016
18 comments
8,226 views