sql server - finding if patient followed up with doctor after 90 days using DataAdd function in SQL -
i have table has 2 datasets, 1 dataset has doctor office data , other 1 has hospital data. trying find out if patient has followed doctor after 90 days initial admission @ hospital. patient can have multiple admission dates , @ each admission separately if followed doctor after 90 days. calculation should be
if service_date - discharge_date > 90 flag 1 set nocount on go declare @test table ( [name] varchar(15) ,[service_dt] datetime ,[dsch_dt] datetime ,[src] varchar(50) ,[loc] varchar(50) ) insert @test ([name], [service_dt], [dsch_dt], [src], [loc]) values ('mike', '2014-01-01', '2014-01-01', 'hospital', 'ny') ,('mike', '2014-2-27', '2014-02-28', 'hospital', 'ny') ,('mike', '2014-07-15', '2014-07-15', 'hospital', 'ny') ,('david', '2014-09-04', '2014-09-04', 'hospital', 'ny') ,('david', '2014-04-01', '2014-04-02', 'hospital', 'ny') ,('tom', '2014-05-01', '2014-08-15', 'hospital', 'tx') ,('mike', '2014-03-02', '9999-01-01', 'doctor_offfice', 'ny' ) ,('mike', '2014-05-22', '9999-01-01', 'doctor_offfice', 'ny' ) ,('david', '2014-01-01', '9999-01-01', 'doctor_offfice', 'ny') ,('david', '2014-05-03', '9999-01-01', 'doctor_offfice', 'ny') ,('tom', '2014-08-20', '9999-01-01', 'doctor_offfice', 'tx') ,('david', '2014-12-02', '9999-01-01', 'doctor_offfice', 'ny') ;with mytest ([name], [service_dt], [dsch_dt], [src], [loc]) ( select name ,[service_dt] ,[dsch_dt] ,[src] ,loc @test ) ,hosp ( select name --,[service_dt] ,[dsch_dt] ,[loc] mytest src = 'hospital' ) ,doc ( select name ,[service_dt] --,[dsch_dt] ,[src] mytest src = 'doctor_offfice' ) select hosp.name, hosp.dsch_dt, doc.service_dt, hosp.loc hosp_src, doc.src doc_src, case when hosp.dsch_dt < dateadd(d, 90, doc.service_dt) 1 else 0 end fu90 hosp inner join doc on hosp.name = doc.name set nocount off go
original fiddle close.
this technically meets provided requirements, doesn't match desired results.
either requirements incorrect (or missing some), or expected results are.
specifically, i'm not sure how account 2 hospital visits , dr visit within 90 days of both...
what does:
- it splits data 2 sets. 1 dr office , 1 hospital. (you did cte fine)
- it joins these sets based on patient name , and having dr visit within 90 days of discharge.
- it evaluates if dr.location null (could evaluate value really) if is, tat means didn't follow-up due left joint works.
.
select h.name, h.service_dt, h.dsch_dt, h.src, h.loc, case when dr.loc not null 1 else 0 end followup90 test h left join (select name, service_dt, dsch_dt, src, loc test src = 'doctor_offfice') dr on dr.name = h.name , dr.service_dt between h.dsch_dt , dateadd(day, 90, h.dsch_dt) h.src = 'hospital' group h.name, h.service_dt, h.dsch_dt, h.src, h.loc, case when dr.loc not null 1 else 0 end order h.name, h.service_dt
as close i'm going without further requirement definition or explanation of why expected results way are. don't conform stated requirements.
--- edit after accepted: account first occurrence, need min service date on subselect , add group by.
however, if followed 2 different doctors on same date, still result in 2 entries being found.
select h.name, h.service_dt, h.dsch_dt, h.src, h.loc, case when dr.loc not null 1 else 0 end followup90 test h left join (select name, min(service_dt) service_dt, loc, src test src = 'doctor_offfice' group name, loc,src ) dr on dr.name = h.name , dr.service_dt between h.dsch_dt , dateadd(day, 90, h.dsch_dt) h.src = 'hospital' group h.name, h.service_dt, h.dsch_dt, h.src, h.loc, case when dr.loc not null 1 else 0 end order h.name, h.service_dt
Comments
Post a Comment