sql - Row_Number Date Range -
i'm trying fill in table has personid appointment date, , whether or not follow call made after visit.
tbl_appt
personid appt_date 1 1/1/2016 1 1/3/2016 1 1/9/2016 1 1/31/2016
tbl_call
personid call_date 1 1/5/2016 1 2/1/2016
desired table: tbl_followup
personid appt_date follow_up_flag 1 1/1/2016 n 1 1/3/2016 y 1 1/9/2016 n 1 1/31/2016 y
the tbl_followup.follow_up_flag = y when tbl_call.call_date > tbl_appt.appt_date , before next appt.
i assume use row_number/partition, these functions allude me (see prior posts).
thanks in advance!
not extremely trivial, assuming have access window functions
such row_number
, here's option using lead
, outer join
:
select a.personid, a.appt_date, case when c.personid null 'n' else 'y' end follow_up_flag ( select personid, appt_date, lead(appt_date) on (partition personid order appt_date) next_appt_date tbl_appt ) left join tbl_call c on c.personid = a.personid , c.call_date > a.appt_date , (c.call_date < a.next_appt_date or a.next_appt_date null) order a.appt_date
Comments
Post a Comment