timscronin
MIS
I have a sp created by a developer from a 3rd party db (can't change the structure) that is used for a crystal report. I understand her need for this logic but it is pretty brutal on the db side. It has to do several unions. Anyone have a better approach?
CREATE procedure cmsproc_rpt_cc_enroll_list @location varchar (50)--passes to patient_.df_sitename
as
select 'CARD' as type, p.last_name, p.first_name,
ltrim(rtrim(upper(p.last_name))) + ', ' + ltrim(rtrim(upper(p.first_name))) as pat_name,
p.other_id_number, p.account_id, l.location_name as location,
p2.df_htn_pt_resp as doc, upper(p2.df_htn_status) as status,
case when isdate(p1.df_htn_enroll_dt)= 1 then convert(datetime,p1.df_htn_enroll_dt,101)
else null end as enroll_dt,
case when isdate(fv.df_nextphyscchtn)= 1 then convert(datetime,fv.df_nextphyscchtn,101)
else null end as next_visit,
case when isdate(p2.df_htn_data_dt)= 1 then convert(datetime,p2.df_htn_data_dt,101)
else null end as database_date
from patient p
inner join patient_ p1 on p1.account_id = p.account_id
inner join location l on l.location_key = p1.df_sitename
inner join follow_up_visits_ fv on fv.account_id = p.account_id
inner join patient2_ p2 on p2.account_id = p.account_id
where p.privacy_level = 10
and p1.df_patstatus = 'Active'
and (p1.df_htn_enroll_dt is not null
or p2.df_htn_pt_resp is not null
or p2.df_htn_status is not null
or fv.df_nextphyscchtn is not null
or p2.df_htn_data_dt is not null)
and (p2.df_htn_disen_dt is null
or p2.df_htn_disen_dt is not null
and p1.df_htn_enroll_dt is not null
and p2.df_htn_disen_dt < p1.df_htn_enroll_dt)
and l.location_name = @location
UNION
select 'ENDO' as type, p.last_name, p.first_name,
ltrim(rtrim(upper(p.last_name))) + ', ' + ltrim(rtrim(upper(p.first_name))) as pat_name,
p.other_id_number, p.account_id, l.location_name as location,
p2.df_dm_pt_resp as doc, upper(p2.df_dm_status) as status,
case when isdate(p1.df_dm_enroll_dt)= 1 then convert(datetime,p1.df_dm_enroll_dt,101)
else null end as enroll_dt,
case when isdate(fv.df_diabccphys)= 1 then convert(datetime,fv.df_diabccphys,101)
else null end as next_visit,
case when isdate(p2.df_dm_data_dt)= 1 then convert(datetime,p2.df_dm_data_dt,101)
else null end as database_date
from patient p
inner join patient_ p1 on p1.account_id = p.account_id
inner join location l on l.location_key = p1.df_sitename
inner join follow_up_visits_ fv on fv.account_id = p.account_id
inner join patient2_ p2 on p2.account_id = p.account_id
where p.privacy_level = 10
and p1.df_patstatus = 'Active'
and (p1.df_dm_enroll_dt is not null
or p2.df_dm_pt_resp is not null
or p2.df_dm_status is not null
or fv.df_diabccphys is not null
or p2.df_dm_data_dt is not null)
and (p2.df_dm_disen_dt is null
or p2.df_dm_disen_dt is not null
and p1.df_dm_enroll_dt is not null
and p2.df_dm_disen_dt < p1.df_dm_enroll_dt)
and l.location_name = @location
UNION
select 'PULM' as type, p.last_name, p.first_name,
ltrim(rtrim(upper(p.last_name))) + ', ' + ltrim(rtrim(upper(p.first_name))) as pat_name,
p.other_id_number, p.account_id, l.location_name as location,
p2.df_asth_pt_resp as doc, upper(p2.df_asth_status) as status,
case when isdate(p1.df_asth_enroll_dt)= 1 then convert(datetime,p1.df_asth_enroll_dt,101)
else null end as enroll_dt,
case when isdate(fv.df_asthma_phys)= 1 then convert(datetime,fv.df_asthma_phys,101)
else null end as next_visit,
case when isdate(p2.df_asth_data_dt)= 1 then convert(datetime,p2.df_dis_data_dt,101)
else null end as database_date
from patient p
inner join patient_ p1 on p1.account_id = p.account_id
inner join location l on l.location_key = p1.df_sitename
inner join follow_up_visits_ fv on fv.account_id = p.account_id
inner join patient2_ p2 on p2.account_id = p.account_id
where p.privacy_level = 10
and p1.df_patstatus = 'Active'
and (p1.df_asth_enroll_dt is not null
or p2.df_asth_pt_resp is not null
or p2.df_asth_status is not null
or fv.df_asthma_phys is not null
or p2.df_asth_data_dt is not null)
and (p2.df_asth_disen_dt is null
or p2.df_asth_disen_dt is not null
and p1.df_asth_enroll_dt is not null
and p2.df_asth_disen_dt < p1.df_asth_enroll_dt)
and l.location_name = @location
UNION
select 'INFDIS' as type, p.last_name, p.first_name,
ltrim(rtrim(upper(p.last_name))) + ', ' + ltrim(rtrim(upper(p.first_name))) as pat_name,
p.other_id_number, p.account_id, l.location_name as location,
p2.df_hiv_pt_resp as doc, upper(p2.df_hiv_status) as status,
case when isdate(p1.df_hiv_enroll_dt)= 1 then convert(datetime,p1.df_hiv_enroll_dt,101)
else null end as enroll_dt,
case when isdate(fv.df_hiv_phys)= 1 then convert(datetime,fv.df_hiv_phys,101)
else null end as next_visit,
case when isdate(p2.df_hiv_data_dt)= 1 then convert(datetime,p2.df_hiv_data_dt,101)
else null end as database_date
from patient p
inner join patient_ p1 on p1.account_id = p.account_id
inner join location l on l.location_key = p1.df_sitename
inner join follow_up_visits_ fv on fv.account_id = p.account_id
inner join patient2_ p2 on p2.account_id = p.account_id
where p.privacy_level = 10
and p1.df_patstatus = 'Active'
and (p1.df_hiv_enroll_dt is not null
or p2.df_hiv_pt_resp is not null
or p2.df_hiv_status is not null
or fv.df_hiv_phys is not null
or p2.df_hiv_data_dt is not null)
and (p2.df_hiv_disen_dt is null
or p2.df_hiv_disen_dt is not null
and p1.df_hiv_enroll_dt is not null
and p2.df_hiv_disen_dt < p1.df_hiv_enroll_dt)
and l.location_name = @location
UNION
select 'NEURO' as type, p.last_name, p.first_name,
ltrim(rtrim(upper(p.last_name))) + ', ' + ltrim(rtrim(upper(p.first_name))) as pat_name,
p.other_id_number, p.account_id, l.location_name as location,
p2.df_sz_pt_resp as doc, upper(p2.df_sz_status) as status,
case when isdate(p1.df_seiz_enroll_dt)= 1 then convert(datetime,p1.df_seiz_enroll_dt,101)
else null end as enroll_dt,
case when isdate(fv.df_phys_seizure)= 1 then convert(datetime,fv.df_phys_seizure,101)
else null end as next_visit,
case when isdate(p2.df_sz_data_dt)= 1 then convert(datetime,p2.df_sz_data_dt,101)
else null end as database_date
from patient p
inner join patient_ p1 on p1.account_id = p.account_id
inner join location l on l.location_key = p1.df_sitename
inner join follow_up_visits_ fv on fv.account_id = p.account_id
inner join patient2_ p2 on p2.account_id = p.account_id
where p.privacy_level = 10
and p1.df_patstatus = 'Active'
and (p1.df_seiz_enroll_dt is not null
or p2.df_sz_pt_resp is not null
or p2.df_sz_status is not null
or fv.df_phys_seizure is not null
or p2.df_sz_data_dt is not null)
and (p2.df_sz_disen_dt is null
or p2.df_sz_disen_dt is not null
and p1.df_seiz_enroll_dt is not null
and p2.df_sz_disen_dt < p1.df_seiz_enroll_dt)
and l.location_name = @location
UNION
select 'GI' as type, p.last_name, p.first_name,
ltrim(rtrim(upper(p.last_name))) + ', ' + ltrim(rtrim(upper(p.first_name))) as pat_name,
p.other_id_number, p.account_id, l.location_name as location,
p2.df_gi_pt_resp as doc, upper(p2.df_gi_status) as status,
case when isdate(p2.df_gi_enroll_dt)= 1 then convert(datetime,p2.df_gi_enroll_dt,101)
else null end as enroll_dt,
case when isdate(fv.df_phys_gastroint)= 1 then convert(datetime,fv.df_phys_gastroint,101)
else null end as next_visit,
case when isdate(p2.df_gi_data_dt)= 1 then convert(datetime,p2.df_gi_data_dt,101)
else null end as database_date
from patient p
inner join patient_ p1 on p1.account_id = p.account_id
inner join location l on l.location_key = p1.df_sitename
inner join follow_up_visits_ fv on fv.account_id = p.account_id
inner join patient2_ p2 on p2.account_id = p.account_id
where p.privacy_level = 10
and p1.df_patstatus = 'Active'
and (p2.df_gi_enroll_dt is not null
or p2.df_gi_pt_resp is not null
or p2.df_gi_status is not null
or fv.df_phys_gastroint is not null
or p2.df_gi_data_dt is not null)
and (p2.df_gi_disen_dt is null
or p2.df_gi_disen_dt is not null
and p2.df_gi_enroll_dt is not null
and p2.df_gi_disen_dt < p2.df_gi_enroll_dt)
and l.location_name = @location
UNION
select 'DIS' as type, p.last_name, p.first_name,
ltrim(rtrim(upper(p.last_name))) + ', ' + ltrim(rtrim(upper(p.first_name))) as pat_name,
p.other_id_number, p.account_id, l.location_name as location,
p2.df_dis_doc as doc, upper(p2.df_dis_status) as status,
case when isdate(p2.df_dis_enroll_dt)= 1 then convert(datetime,p2.df_dis_enroll_dt,101)
else null end as enroll_dt,
case when isdate(fv.df_phys_disability)= 1 then convert(datetime,fv.df_phys_disability,101)
else null end as next_visit,
case when isdate(p2.df_dis_data_dt)= 1 then convert(datetime,p2.df_dis_data_dt,101)
else null end as database_date
from patient p
inner join patient_ p1 on p1.account_id = p.account_id
inner join location l on l.location_key = p1.df_sitename
inner join follow_up_visits_ fv on fv.account_id = p.account_id
inner join patient2_ p2 on p2.account_id = p.account_id
where p.privacy_level = 10
and p1.df_patstatus = 'Active'
and (p2.df_dis_enroll_dt is not null
or p2.df_dis_doc is not null
or p2.df_dis_status is not null
or fv.df_phys_disability is not null
or p2.df_dis_data_dt is not null)
and (p2.df_dis_disen_dt is null
or p2.df_dis_disen_dt is not null
and p2.df_dis_enroll_dt is not null
and p2.df_dis_disen_dt < p2.df_dis_enroll_dt)
and l.location_name = @location
order by 1,2,3 -- type, p.last_name, p.first_name
CREATE procedure cmsproc_rpt_cc_enroll_list @location varchar (50)--passes to patient_.df_sitename
as
select 'CARD' as type, p.last_name, p.first_name,
ltrim(rtrim(upper(p.last_name))) + ', ' + ltrim(rtrim(upper(p.first_name))) as pat_name,
p.other_id_number, p.account_id, l.location_name as location,
p2.df_htn_pt_resp as doc, upper(p2.df_htn_status) as status,
case when isdate(p1.df_htn_enroll_dt)= 1 then convert(datetime,p1.df_htn_enroll_dt,101)
else null end as enroll_dt,
case when isdate(fv.df_nextphyscchtn)= 1 then convert(datetime,fv.df_nextphyscchtn,101)
else null end as next_visit,
case when isdate(p2.df_htn_data_dt)= 1 then convert(datetime,p2.df_htn_data_dt,101)
else null end as database_date
from patient p
inner join patient_ p1 on p1.account_id = p.account_id
inner join location l on l.location_key = p1.df_sitename
inner join follow_up_visits_ fv on fv.account_id = p.account_id
inner join patient2_ p2 on p2.account_id = p.account_id
where p.privacy_level = 10
and p1.df_patstatus = 'Active'
and (p1.df_htn_enroll_dt is not null
or p2.df_htn_pt_resp is not null
or p2.df_htn_status is not null
or fv.df_nextphyscchtn is not null
or p2.df_htn_data_dt is not null)
and (p2.df_htn_disen_dt is null
or p2.df_htn_disen_dt is not null
and p1.df_htn_enroll_dt is not null
and p2.df_htn_disen_dt < p1.df_htn_enroll_dt)
and l.location_name = @location
UNION
select 'ENDO' as type, p.last_name, p.first_name,
ltrim(rtrim(upper(p.last_name))) + ', ' + ltrim(rtrim(upper(p.first_name))) as pat_name,
p.other_id_number, p.account_id, l.location_name as location,
p2.df_dm_pt_resp as doc, upper(p2.df_dm_status) as status,
case when isdate(p1.df_dm_enroll_dt)= 1 then convert(datetime,p1.df_dm_enroll_dt,101)
else null end as enroll_dt,
case when isdate(fv.df_diabccphys)= 1 then convert(datetime,fv.df_diabccphys,101)
else null end as next_visit,
case when isdate(p2.df_dm_data_dt)= 1 then convert(datetime,p2.df_dm_data_dt,101)
else null end as database_date
from patient p
inner join patient_ p1 on p1.account_id = p.account_id
inner join location l on l.location_key = p1.df_sitename
inner join follow_up_visits_ fv on fv.account_id = p.account_id
inner join patient2_ p2 on p2.account_id = p.account_id
where p.privacy_level = 10
and p1.df_patstatus = 'Active'
and (p1.df_dm_enroll_dt is not null
or p2.df_dm_pt_resp is not null
or p2.df_dm_status is not null
or fv.df_diabccphys is not null
or p2.df_dm_data_dt is not null)
and (p2.df_dm_disen_dt is null
or p2.df_dm_disen_dt is not null
and p1.df_dm_enroll_dt is not null
and p2.df_dm_disen_dt < p1.df_dm_enroll_dt)
and l.location_name = @location
UNION
select 'PULM' as type, p.last_name, p.first_name,
ltrim(rtrim(upper(p.last_name))) + ', ' + ltrim(rtrim(upper(p.first_name))) as pat_name,
p.other_id_number, p.account_id, l.location_name as location,
p2.df_asth_pt_resp as doc, upper(p2.df_asth_status) as status,
case when isdate(p1.df_asth_enroll_dt)= 1 then convert(datetime,p1.df_asth_enroll_dt,101)
else null end as enroll_dt,
case when isdate(fv.df_asthma_phys)= 1 then convert(datetime,fv.df_asthma_phys,101)
else null end as next_visit,
case when isdate(p2.df_asth_data_dt)= 1 then convert(datetime,p2.df_dis_data_dt,101)
else null end as database_date
from patient p
inner join patient_ p1 on p1.account_id = p.account_id
inner join location l on l.location_key = p1.df_sitename
inner join follow_up_visits_ fv on fv.account_id = p.account_id
inner join patient2_ p2 on p2.account_id = p.account_id
where p.privacy_level = 10
and p1.df_patstatus = 'Active'
and (p1.df_asth_enroll_dt is not null
or p2.df_asth_pt_resp is not null
or p2.df_asth_status is not null
or fv.df_asthma_phys is not null
or p2.df_asth_data_dt is not null)
and (p2.df_asth_disen_dt is null
or p2.df_asth_disen_dt is not null
and p1.df_asth_enroll_dt is not null
and p2.df_asth_disen_dt < p1.df_asth_enroll_dt)
and l.location_name = @location
UNION
select 'INFDIS' as type, p.last_name, p.first_name,
ltrim(rtrim(upper(p.last_name))) + ', ' + ltrim(rtrim(upper(p.first_name))) as pat_name,
p.other_id_number, p.account_id, l.location_name as location,
p2.df_hiv_pt_resp as doc, upper(p2.df_hiv_status) as status,
case when isdate(p1.df_hiv_enroll_dt)= 1 then convert(datetime,p1.df_hiv_enroll_dt,101)
else null end as enroll_dt,
case when isdate(fv.df_hiv_phys)= 1 then convert(datetime,fv.df_hiv_phys,101)
else null end as next_visit,
case when isdate(p2.df_hiv_data_dt)= 1 then convert(datetime,p2.df_hiv_data_dt,101)
else null end as database_date
from patient p
inner join patient_ p1 on p1.account_id = p.account_id
inner join location l on l.location_key = p1.df_sitename
inner join follow_up_visits_ fv on fv.account_id = p.account_id
inner join patient2_ p2 on p2.account_id = p.account_id
where p.privacy_level = 10
and p1.df_patstatus = 'Active'
and (p1.df_hiv_enroll_dt is not null
or p2.df_hiv_pt_resp is not null
or p2.df_hiv_status is not null
or fv.df_hiv_phys is not null
or p2.df_hiv_data_dt is not null)
and (p2.df_hiv_disen_dt is null
or p2.df_hiv_disen_dt is not null
and p1.df_hiv_enroll_dt is not null
and p2.df_hiv_disen_dt < p1.df_hiv_enroll_dt)
and l.location_name = @location
UNION
select 'NEURO' as type, p.last_name, p.first_name,
ltrim(rtrim(upper(p.last_name))) + ', ' + ltrim(rtrim(upper(p.first_name))) as pat_name,
p.other_id_number, p.account_id, l.location_name as location,
p2.df_sz_pt_resp as doc, upper(p2.df_sz_status) as status,
case when isdate(p1.df_seiz_enroll_dt)= 1 then convert(datetime,p1.df_seiz_enroll_dt,101)
else null end as enroll_dt,
case when isdate(fv.df_phys_seizure)= 1 then convert(datetime,fv.df_phys_seizure,101)
else null end as next_visit,
case when isdate(p2.df_sz_data_dt)= 1 then convert(datetime,p2.df_sz_data_dt,101)
else null end as database_date
from patient p
inner join patient_ p1 on p1.account_id = p.account_id
inner join location l on l.location_key = p1.df_sitename
inner join follow_up_visits_ fv on fv.account_id = p.account_id
inner join patient2_ p2 on p2.account_id = p.account_id
where p.privacy_level = 10
and p1.df_patstatus = 'Active'
and (p1.df_seiz_enroll_dt is not null
or p2.df_sz_pt_resp is not null
or p2.df_sz_status is not null
or fv.df_phys_seizure is not null
or p2.df_sz_data_dt is not null)
and (p2.df_sz_disen_dt is null
or p2.df_sz_disen_dt is not null
and p1.df_seiz_enroll_dt is not null
and p2.df_sz_disen_dt < p1.df_seiz_enroll_dt)
and l.location_name = @location
UNION
select 'GI' as type, p.last_name, p.first_name,
ltrim(rtrim(upper(p.last_name))) + ', ' + ltrim(rtrim(upper(p.first_name))) as pat_name,
p.other_id_number, p.account_id, l.location_name as location,
p2.df_gi_pt_resp as doc, upper(p2.df_gi_status) as status,
case when isdate(p2.df_gi_enroll_dt)= 1 then convert(datetime,p2.df_gi_enroll_dt,101)
else null end as enroll_dt,
case when isdate(fv.df_phys_gastroint)= 1 then convert(datetime,fv.df_phys_gastroint,101)
else null end as next_visit,
case when isdate(p2.df_gi_data_dt)= 1 then convert(datetime,p2.df_gi_data_dt,101)
else null end as database_date
from patient p
inner join patient_ p1 on p1.account_id = p.account_id
inner join location l on l.location_key = p1.df_sitename
inner join follow_up_visits_ fv on fv.account_id = p.account_id
inner join patient2_ p2 on p2.account_id = p.account_id
where p.privacy_level = 10
and p1.df_patstatus = 'Active'
and (p2.df_gi_enroll_dt is not null
or p2.df_gi_pt_resp is not null
or p2.df_gi_status is not null
or fv.df_phys_gastroint is not null
or p2.df_gi_data_dt is not null)
and (p2.df_gi_disen_dt is null
or p2.df_gi_disen_dt is not null
and p2.df_gi_enroll_dt is not null
and p2.df_gi_disen_dt < p2.df_gi_enroll_dt)
and l.location_name = @location
UNION
select 'DIS' as type, p.last_name, p.first_name,
ltrim(rtrim(upper(p.last_name))) + ', ' + ltrim(rtrim(upper(p.first_name))) as pat_name,
p.other_id_number, p.account_id, l.location_name as location,
p2.df_dis_doc as doc, upper(p2.df_dis_status) as status,
case when isdate(p2.df_dis_enroll_dt)= 1 then convert(datetime,p2.df_dis_enroll_dt,101)
else null end as enroll_dt,
case when isdate(fv.df_phys_disability)= 1 then convert(datetime,fv.df_phys_disability,101)
else null end as next_visit,
case when isdate(p2.df_dis_data_dt)= 1 then convert(datetime,p2.df_dis_data_dt,101)
else null end as database_date
from patient p
inner join patient_ p1 on p1.account_id = p.account_id
inner join location l on l.location_key = p1.df_sitename
inner join follow_up_visits_ fv on fv.account_id = p.account_id
inner join patient2_ p2 on p2.account_id = p.account_id
where p.privacy_level = 10
and p1.df_patstatus = 'Active'
and (p2.df_dis_enroll_dt is not null
or p2.df_dis_doc is not null
or p2.df_dis_status is not null
or fv.df_phys_disability is not null
or p2.df_dis_data_dt is not null)
and (p2.df_dis_disen_dt is null
or p2.df_dis_disen_dt is not null
and p2.df_dis_enroll_dt is not null
and p2.df_dis_disen_dt < p2.df_dis_enroll_dt)
and l.location_name = @location
order by 1,2,3 -- type, p.last_name, p.first_name