SQL (Oracle) to query for record with max date, only if the end_dt has a value -
i trying select record row looking @ both start date , end date. need pick max start date, return result max date if end date has value.
i hope images below clarify bit more. in oracle based sql.
example #2
i can, far, either return records or incorrectly return record in scenario #2 i've yet figure out best way make work. greatly appreciate assistance.
thank you!
i use analytic function:
with sample_data (select 1 id, 1 grp_id, to_date('01/01/2015', 'dd/mm/yyyy') st_dt, to_date('23/01/2015', 'dd/mm/yyyy') ed_dt dual union select 2 id, 1 grp_id, to_date('24/02/2015', 'dd/mm/yyyy') st_dt, to_date('15/02/2015', 'dd/mm/yyyy') ed_dt dual union select 3 id, 1 grp_id, to_date('17/03/2015', 'dd/mm/yyyy') st_dt, to_date('30/03/2015', 'dd/mm/yyyy') ed_dt dual union select 4 id, 2 grp_id, to_date('01/01/2015', 'dd/mm/yyyy') st_dt, to_date('17/01/2015', 'dd/mm/yyyy') ed_dt dual union select 5 id, 2 grp_id, to_date('21/01/2015', 'dd/mm/yyyy') st_dt, to_date('23/03/2015', 'dd/mm/yyyy') ed_dt dual union select 6 id, 2 grp_id, to_date('14/04/2015', 'dd/mm/yyyy') st_dt, to_date('16/05/2015', 'dd/mm/yyyy') ed_dt dual union select 7 id, 2 grp_id, to_date('28/05/2015', 'dd/mm/yyyy') st_dt, null ed_dt dual), res (select id, grp_id, st_dt, ed_dt, max(st_dt) on (partition grp_id) max_st_dt sample_data) select id, grp_id, st_dt, ed_dt res st_dt = max_st_dt , ed_dt not null; id grp_id st_dt ed_dt ---------- ---------- ---------- ---------- 3 1 17/03/2015 30/03/2015
Comments
Post a Comment