Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 8451

Command Line SQL Help

$
0
0

The report below is currently set to run for  1 account. It is currently running for hours but when I remove 1 of the inline views it runs in seconds.  I removed the sql in red below.  It doesn't matter what view I remove it still runs in seconds.  Is there a way to fix this?

 

Command Line

WITH
--temp table base_pats that retrieves the episode data. This data is retrieved only once and referenced throughout the subqueries in the main query below
base_pats as
(
select distinct
    penc.pat_enc_csn_id,  penc.pat_id, penc.contact_date, edg9.code,edg9.dx_id, epi.episode_id, zep_type.name as epi_type, zep_status.name as epi_status, vf.mrn,
    vf.pat_name, payor.payor_name, rfsrc.referring_prov_nam, floor((penc.contact_date - pat.birth_date)/365.25) as Age, epi.name as episode_name, epi.start_date as epi_start,  epi.end_date as epi_end    
   
from episode epi
  left outer join episode_link elink on epi.episode_id = elink.episode_id
  left outer join pat_enc penc on elink.pat_enc_csn_id = penc.pat_enc_csn_id
  left outer join pat_enc_dx pdx on penc.pat_enc_csn_id = pdx.pat_enc_csn_id
  inner join edg_current_icd9 edg9 on pdx.dx_id = edg9.dx_id       -- the LBP codes retrieved in the temp table above are used to isolate the patient encounter visits
  left outer join clarity_dep dep on penc.department_id = dep.department_id
  left outer join clarity_epm payor on penc.visit_epm_id = payor.payor_id
  left outer join epicadm.v_fv_mrn vf on penc.pat_id = vf.pat_id
  left outer join patient pat on penc.pat_id = pat.pat_id
  left outer join referral_source rfsrc on penc.referral_source_id = rfsrc.referring_prov_id
  left outer join ZC_SUM_BLK_TYPE zep_type on epi.SUM_BLK_TYPE_ID = zep_type.SUM_BLK_TYPE_ID
  left outer join ZC_EPI_STATUS zep_status on epi.status_c = zep_status.epi_status_c

where  (penc.contact_date between TO_DATE ('2015/01/01', 'yyyy/mm/dd') and
TO_DATE('2015/03/31', 'yyyy/mm/dd'))
and vf.mrn = '000000A42'
--and penc.department_id in ('602002','643001') -- for testing only
and dep.rpt_grp_eleven_c = '122'    -- IAM department report grouper
--{?Start Date} AND {?End Date})
and  penc.enc_type_c = '5000'
--and  (0 in {?Department} or penc.department_id in {?Department})
and penc.appt_status_c not in (3,4,5,9)
and
(
edg9.code in ('720.1','721.3', '721.39','721.42','721.9','721.90','722.6','722.69','722.93','724.39','724.5','724.69','724.70','724.71','724.79','738.5','738.59','739.3',
'739.49','846.39','846.8','846.99','847.2','847.29','738.4', '724.3','724.7','846.1','846.2','846.3','846.8','846.9')
or
(edg9.code like '722.10%' or edg9.code like '722.52%'
or edg9.code like '724.02%' or edg9.code like '724.2%' or edg9.code like '846.0%'  or
edg9.code like '847.2%' or edg9.code like '724.4%'
or edg9.code like '724.6%')
)
)
--Main Query that uses the temp tables above
SELECT 
base_pats.pat_enc_csn_id,  base_pats.pat_id, base_pats.contact_date, base_pats.code,base_pats.dx_id, base_pats.episode_id, base_pats.epi_type, base_pats.epi_status, base_pats.mrn, base_pats.pat_name,
base_pats.payor_name, base_pats.referring_prov_nam, base_pats.Age, base_pats.episode_name, base_pats.epi_start, base_pats.epi_end,

-- Flowsheet data
Earliest_OSW.first_osw_score,Earliest_OSW.first_osw_date,Earliest_OSW.first_osw_prov,Earliest_OSW.first_osw_dept,
Latest_OSW.last_osw_score,Latest_OSW.last_osw_date,Latest_OSW.last_osw_prov,Latest_OSW.last_osw_dept,
Earliest_Keele_Sub.early_keele_sub_score as first_Keele_Sub_score,Earliest_Keele_Tot.early_keele_tot_score  as first_Keele_Total_score,
Earliest_Keele_Sub.early_keele_sub_date as first_Keele_date,Earliest_Keele_Sub.early_keele_dept as first_Keele_dept,
Latest_Keele_Sub.last_keele_sub_score as last_Keele_Sub_score,Latest_Keele_Tot.last_keele_tot_score as last_Keele_Total_score,
Latest_Keele_Sub.last_keele_sub_date as last_Keele_date,Latest_Keele_Sub.last_keele_dept as last_Keele_dept,
Latest_Pam_Level.last_PAM_level as Pam_level,
to_number(Latest_Pam_Score.last_PAM_score) as Pam_score,
Latest_Pam_Level.last_PAM_level_date as Pam_contact_date,Latest_Pam_Level.last_PAM_dept as Pam_score_dept,

epis_visits.num as episode_visits,

--the charge data procedure quantity, charge amount in the date range and charge amount so far in the episode
AC.proc_qty as  Active_Care, AC.proc_amt as  Active_Care_Amt, AC_Epis.proc_amt as Active_Care_Amt_Epis,
MC.proc_qty as  Manual_Care, MC.proc_amt as  Manual_Care_Amt, MC_Epis.proc_amt as Manual_Care_Amt_Epis,
MD.proc_qty as  Modalities, MD.proc_amt as  Modalities_Amt, MD_Epis.proc_amt as Modalities_Amt_Epis,
Trac.proc_qty as  Traction, Trac.proc_amt as  Traction_Amt,

--Trac_Epis.proc_amt as Traction_Amt_Epis,

Eval_Epis.proc_amt as Eval_Codes_Amt,
Hot_packs.hp_count as Hot_Packs_Ordered,
surg_code.scode as surgical_code

FROM base_pats base_pats

--This retrieves the earliest in the episode Oswestry flowsheet measure and related data. The row_number function sorts the data by ascending order based on recorded time to get the earliest value. Also check to ensure the data is not earlier than report end date
left outer join (  select epi2.episode_id, vflo_oswestry_2.meas_value as first_osw_score, vflo_oswestry_2.recorded_time as first_osw_date, ser2.prov_name as first_osw_prov,
                         dep2.department_name as first_osw_dept, row_number() over(partition by epi2.episode_id order by vflo_oswestry_2.recorded_time asc NULLS last) ENC_RANK
                         from episode epi2
                         left outer join episode_link elink2 on epi2.episode_id = elink2.episode_id
                         left outer join epicadm.v_fv_flo_meas_op vflo_oswestry_2 on elink2.pat_enc_csn_id = vflo_oswestry_2.pat_enc_csn_id and vflo_oswestry_2.flo_meas_id = '3977'
                                  and (vflo_oswestry_2.recorded_time <= TO_DATE('2015/03/31', 'yyyy/mm/dd'))
                         left outer join pat_enc penc2 on vflo_oswestry_2.pat_enc_csn_id = penc2.pat_enc_csn_id
                         left outer join clarity_ser ser2 on penc2.visit_prov_id = ser2.prov_id
                         left outer join clarity_dep dep2 on penc2.department_id = dep2.department_id
                       ) Earliest_OSW on base_pats.episode_id = Earliest_OSW.episode_id  and Earliest_OSW.enc_rank = 1

--This retrieves the latest in the episode Oswestry flowsheet measure and related data. The row_number function sorts the data by descending order based on recorded time to get the latest value. Also check to ensure the data is not earlier than report end date
left outer join (  select epi3.episode_id, vflo_oswestry_3.meas_value as last_osw_score, vflo_oswestry_3.recorded_time as last_osw_date, ser3.prov_name as last_osw_prov,
                         dep3.department_name as last_osw_dept, row_number() over(partition by epi3.episode_id order by vflo_oswestry_3.recorded_time desc nulls last) ENC_RANK
                         from episode epi3
                         left outer join episode_link elink3 on epi3.episode_id = elink3.episode_id
                         left outer join epicadm.v_fv_flo_meas_op vflo_oswestry_3 on elink3.pat_enc_csn_id = vflo_oswestry_3.pat_enc_csn_id
                                and vflo_oswestry_3.flo_meas_id = '3977' and (vflo_oswestry_3.recorded_time <= TO_DATE('2015/03/31', 'yyyy/mm/dd'))
                                --{?End Date})
                         left outer join pat_enc penc3 on vflo_oswestry_3.pat_enc_csn_id = penc3.pat_enc_csn_id
                         left outer join clarity_ser ser3 on penc3.visit_prov_id = ser3.prov_id
                         left outer join clarity_dep dep3 on penc3.department_id = dep3.department_id
                         ) Latest_OSW on base_pats.episode_id = Latest_OSW.episode_id and Latest_OSW.enc_rank = 1

--This retrieves the earliest in the episode Keele Sub flowsheet measure and related data. The row_number function sorts the data by ascending order based on recorded time to get the earliest value. Also check to ensure the data is not earlier than report end date
  left outer join (  select epi2.episode_id, vflo_keele_sub.meas_value as early_keele_sub_score, vflo_keele_sub.recorded_time as early_keele_sub_date, ser2.prov_name as early_keele_prov,
                         dep2.department_name as early_keele_dept, row_number() over(partition by epi2.episode_id order by vflo_keele_sub.recorded_time asc NULLS last) ENC_RANK
                         from episode epi2
                         left outer join episode_link elink2 on epi2.episode_id = elink2.episode_id
                         left outer join epicadm.v_fv_flo_meas_op vflo_keele_sub on elink2.pat_enc_csn_id = vflo_keele_sub.pat_enc_csn_id
                               and vflo_keele_sub.flo_meas_id = '6408' and (vflo_keele_sub.recorded_time <= TO_DATE('2015/03/31', 'yyyy/mm/dd'))
                               --{?End Date})
                         left outer join pat_enc penc2 on vflo_keele_sub.pat_enc_csn_id = penc2.pat_enc_csn_id
                         left outer join clarity_ser ser2 on penc2.visit_prov_id = ser2.prov_id
                         left outer join clarity_dep dep2 on penc2.department_id = dep2.department_id
                        ) Earliest_Keele_Sub on base_pats.episode_id = Earliest_Keele_sub.episode_id and Earliest_keele_sub.enc_rank = 1
 
--This retrieves the latest in the episode Keele Sub flowsheet measure and related data. The row_number function sorts the data by descending order based on recorded time to get the latest value. Also check to ensure the data is not earlier than report end date                     
  left outer join (  select epi2.episode_id, vflo_keele_sub.meas_value as last_keele_sub_score, vflo_keele_sub.recorded_time as last_keele_sub_date, ser2.prov_name as last_keele_prov,
                         dep2.department_name as last_keele_dept, row_number() over(partition by epi2.episode_id order by vflo_keele_sub.recorded_time desc NULLS last) ENC_RANK
                         from episode epi2
                         left outer join episode_link elink2 on epi2.episode_id = elink2.episode_id
                        left outer join epicadm.v_fv_flo_meas_op vflo_keele_sub on elink2.pat_enc_csn_id = vflo_keele_sub.pat_enc_csn_id
                               and vflo_keele_sub.flo_meas_id = '6408' and (vflo_keele_sub.recorded_time <= TO_DATE('2015/03/31', 'yyyy/mm/dd'))
                               --{?End Date})
                         left outer join pat_enc penc2 on vflo_keele_sub.pat_enc_csn_id = penc2.pat_enc_csn_id
                         left outer join clarity_ser ser2 on penc2.visit_prov_id = ser2.prov_id
                         left outer join clarity_dep dep2 on penc2.department_id = dep2.department_id
                        ) Latest_Keele_Sub on base_pats.episode_id = Latest_Keele_sub.episode_id and Latest_keele_sub.enc_rank = 1

--This retrieves the earliest in the episode Keele Total flowsheet measure and related data. The row_number function sorts the data by ascending order based on recorded time to get the earliest value. Also check to ensure the data is not earlier than report end date
left outer join (  select epi2.episode_id, vflo_keele_tot.meas_value as early_keele_tot_score, vflo_keele_tot.recorded_time as early_keele_tot_date, 
                         row_number() over(partition by epi2.episode_id order by vflo_keele_tot.recorded_time asc NULLS last) ENC_RANK
                         from episode epi2
                         left outer join episode_link elink2 on epi2.episode_id = elink2.episode_id
                         left outer join epicadm.v_fv_flo_meas_op vflo_keele_tot on elink2.pat_enc_csn_id = vflo_keele_tot.pat_enc_csn_id
                              and vflo_keele_tot.flo_meas_id = '6411' and (vflo_keele_tot.recorded_time <= TO_DATE('2015/03/31', 'yyyy/mm/dd'))
                              --{?End Date})
                         left outer join pat_enc penc2 on vflo_keele_tot.pat_enc_csn_id = penc2.pat_enc_csn_id
                      ) Earliest_Keele_Tot on base_pats.episode_id = Earliest_Keele_tot.episode_id and Earliest_keele_tot.enc_rank = 1
 
--This retrieves the latest in the episode Keele Total flowsheet measure and related data. The row_number function sorts the data by descending order based on recorded time to get the latest value. Also check to ensure the data is not earlier than report end date                 
  left outer join (  select epi2.episode_id, vflo_keele_tot.meas_value as last_keele_tot_score, vflo_keele_tot.recorded_time as last_keele_tot_date, 
                         row_number() over(partition by epi2.episode_id order by vflo_keele_tot.recorded_time desc NULLS last) ENC_RANK
                         from episode epi2
                         left outer join episode_link elink2 on epi2.episode_id = elink2.episode_id
                         left outer join epicadm.v_fv_flo_meas_op vflo_keele_tot on elink2.pat_enc_csn_id = vflo_keele_tot.pat_enc_csn_id
                            and vflo_keele_tot.flo_meas_id = '6411' and (vflo_keele_tot.recorded_time <= TO_DATE('2015/03/31', 'yyyy/mm/dd'))
                            --{?End Date})
                         left outer join pat_enc penc2 on vflo_keele_tot.pat_enc_csn_id = penc2.pat_enc_csn_id
                      ) Latest_Keele_Tot on base_pats.episode_id = Latest_Keele_tot.episode_id and Latest_keele_tot.enc_rank = 1
                     
--This retrieves the latest PAM Level flowsheet measure and related data. PAM data can be recorded in non-IAM visits hence the join using the patient ID rather than the IAM episode ID
left outer join ( select vflo_pam_level.pat_id, vflo_pam_level.meas_value as last_PAM_level, vflo_pam_level.recorded_time as last_PAM_level_date, dep2.department_name as last_PAM_dept,
                       row_number() over(partition by vflo_pam_level.pat_id order by vflo_pam_level.recorded_time desc NULLS last) ENC_RANK
                       from epicadm.v_fv_flo_meas_op vflo_pam_level
                      left outer join pat_enc penc2 on vflo_pam_level.pat_enc_csn_id = penc2.pat_enc_csn_id
                      left outer join clarity_dep dep2 on penc2.department_id = dep2.department_id
                      where vflo_pam_level.flo_meas_id ='1095'
                      and (vflo_pam_level.recorded_time <= TO_DATE('2015/03/31', 'yyyy/mm/dd'))
                      --{?End Date})
                     ) Latest_Pam_Level on base_pats.pat_id = Latest_Pam_Level.pat_id and Latest_Pam_Level.enc_rank = 1

--This retrieves the latest PAM Score flowsheet measure and related data. PAM data can be recorded in non-IAM visits hence the join using the patient ID rather than the IAM episode ID                    
left outer join ( select vflo_pam_score.pat_id, vflo_pam_score.meas_value as last_PAM_score, vflo_pam_score.recorded_time as last_PAM_score_date,
                       row_number() over(partition by vflo_pam_score.pat_id order by vflo_pam_score.recorded_time desc NULLS last) ENC_RANK
                       from epicadm.v_fv_flo_meas_op vflo_pam_score
                      left outer join pat_enc penc2 on vflo_pam_score.pat_enc_csn_id = penc2.pat_enc_csn_id
                      where vflo_pam_score.flo_meas_id ='1094'
                      and (vflo_pam_score.recorded_time <= TO_DATE('2015/03/31', 'yyyy/mm/dd'))
                      --{?End Date})
                     ) Latest_Pam_Score on base_pats.pat_id = Latest_Pam_Score.pat_id and Latest_Pam_Score.enc_rank = 1

-- Sub query that counts the number of patient encounters in the episode within the start and end date. Note that the encounters are also filtered for the LBP diagnosis codes (using the CODES temp table)
left outer join
    (select visits.episode_id, count(visits.pat_enc_csn_id) as num
        from
        (select distinct penc.pat_enc_csn_id, elink.episode_id
        from episode_link elink
        left outer join pat_enc penc on elink.pat_enc_csn_id = penc.pat_enc_csn_id
        left outer join pat_enc_dx pdx on penc.pat_enc_csn_id = pdx.pat_enc_csn_id
        inner join edg_current_icd9 icd9 on pdx.dx_id = icd9.dx_id
        where (penc.contact_date between TO_DATE ('2015/01/01', 'yyyy/mm/dd') and
        TO_DATE('2015/03/31', 'yyyy/mm/dd'))
        and penc.enc_type_c = '5000'
        --{?Start Date} AND {?End Date})
        and penc.appt_status_c not in (3,4,5,9)
    )visits
    group by visits.episode_id) epis_visits on base_pats.episode_id = epis_visits.episode_id
   
-- Sub query that counts the number of Hot Packs ordered in the date range
left outer join
   (
     select elink.episode_id, count(elink.pat_enc_csn_id) as HP_count
      from episode_link elink
      left outer join order_proc ord   on elink.pat_enc_csn_id = ord.pat_enc_csn_id
      where (ord.ordering_date between TO_DATE ('2015/01/01', 'yyyy/mm/dd') and
TO_DATE('2015/03/31', 'yyyy/mm/dd'))
      --{?Start Date} AND {?End Date})
      and ord.proc_code = '97010'
      group by elink.episode_id
   ) Hot_Packs on base_pats.episode_id = hot_packs.episode_id
  
-- This is the total quantity and charge totals of Active Care codes in the episode performed within the report date range. Each transaction (tdl.tx_id) is also evaluated to ensure it has the LBP codes identified in the CODES temp table
left outer join
    (select elink.episode_id, sum(tdl.procedure_quantity) as proc_qty, sum(tdl.amount) as proc_amt
   from episode_link elink
   left outer join clarity_tdl_tran tdl on elink.pat_enc_csn_id = tdl.pat_enc_csn_id
   join (   select distinct vdx.tx_id, arpb.pat_enc_csn_id
              from EPICADM.arpb_transactions arpb
              join epicadm.tx_diag vdx on arpb.tx_id = vdx.tx_id
              inner join edg_current_icd9 icd9 on vdx.dx_id = icd9.dx_id
         ) txcodes on tdl.pat_enc_csn_id = txcodes.pat_enc_csn_id and tdl.tx_id = txcodes.tx_id
   where tdl.cpt_code in ('97116','97112','97110','97530','97537','97535')   -- CPT codes classified as Active Care by IAM
   --and (tdl.orig_service_date between {?Start Date} AND {?End Date})
   and (tdl.orig_service_date between TO_DATE ('2015/01/01', 'yyyy/mm/dd') and
TO_DATE('2015/03/31', 'yyyy/mm/dd'))

   --{?Start Date} AND {?End Date})
   and tdl.detail_type in ( '1','10')
   group by elink.episode_id) AC on base_pats.episode_id = AC.episode_id

-- This is the total quantity and charge totals of all Active Care codes in the episode performed between episode start date and report end date. Each transaction (tdl.tx_id) is also evaluated to ensure it has the LBP codes identified in the CODES temp table
left outer join
    (select elink.episode_id, sum(tdl.amount) as proc_amt
   from episode epi
   left outer join episode_link elink on epi.episode_id = elink.episode_id
   left outer join clarity_tdl_tran tdl on elink.pat_enc_csn_id = tdl.pat_enc_csn_id
   join (   select distinct vdx.tx_id, arpb.pat_enc_csn_id
              from EPICADM.arpb_transactions arpb
              join epicadm.tx_diag vdx on arpb.tx_id = vdx.tx_id
              inner join edg_current_icd9 icd9 on vdx.dx_id = icd9.dx_id
         ) txcodes on tdl.pat_enc_csn_id = txcodes.pat_enc_csn_id and tdl.tx_id = txcodes.tx_id
   where tdl.cpt_code in ('97116','97112','97110','97530','97537','97535')    -- CPT codes classified as Active Care by IAM
   --and (tdl.orig_service_date <= {?End Date})
   and (tdl.orig_service_date between epi.start_date AND TO_DATE('2015/03/31', 'yyyy/mm/dd'))
   --{?End Date})
   and tdl.detail_type in ( '1','10')
   group by elink.episode_id) AC_Epis on base_pats.episode_id = AC_Epis.episode_id
  
-- This is the total quantity and charge totals of Manual Care codes in the episode performed within the report date range. Each transaction (tdl.tx_id) is also evaluated to ensure it has the LBP codes identified in the CODES temp table  
left outer join
    (select elink.episode_id, sum(tdl.procedure_quantity) as proc_qty, sum(tdl.amount) as proc_amt
   from episode_link elink
   left outer join clarity_tdl_tran tdl on elink.pat_enc_csn_id = tdl.pat_enc_csn_id
   join (   select distinct vdx.tx_id, arpb.pat_enc_csn_id
              from EPICADM.arpb_transactions arpb
              join epicadm.tx_diag vdx on arpb.tx_id = vdx.tx_id
              inner join edg_current_icd9 icd9 on vdx.dx_id = icd9.dx_id
         ) txcodes on tdl.pat_enc_csn_id = txcodes.pat_enc_csn_id and tdl.tx_id = txcodes.tx_id
   where tdl.cpt_code = '97140'       -- CPT code classified as Manual Care by IAM
   --and (tdl.orig_service_date between {?Start Date} AND {?End Date})
   and (tdl.orig_service_date between TO_DATE ('2015/01/01', 'yyyy/mm/dd') and
TO_DATE('2015/03/31', 'yyyy/mm/dd'))
   --{?Start Date} AND {?End Date})
   and tdl.detail_type in ( '1','10')
   group by elink.episode_id) MC on base_pats.episode_id = MC.episode_id

-- This is the total quantity and charge totals of all Manual Care codes in the episode between episode start date and report end date. Each transaction (tdl.tx_id) is also evaluated to ensure it has the LBP codes identified in the CODES temp table
left outer join
    (select elink.episode_id, sum(tdl.amount) as proc_amt
   from episode epi
   left outer join episode_link elink on epi.episode_id = elink.episode_id
   left outer join clarity_tdl_tran tdl on elink.pat_enc_csn_id = tdl.pat_enc_csn_id
   join (   select distinct vdx.tx_id, arpb.pat_enc_csn_id
             from EPICADM.arpb_transactions arpb
              join epicadm.tx_diag vdx on arpb.tx_id = vdx.tx_id
              inner join edg_current_icd9 icd9 on vdx.dx_id = icd9.dx_id
         ) txcodes on tdl.pat_enc_csn_id = txcodes.pat_enc_csn_id and tdl.tx_id = txcodes.tx_id
   where tdl.cpt_code = '97140'   
   --and (tdl.orig_service_date <= {?End Date})
   and (tdl.orig_service_date between epi.start_date AND TO_DATE('2015/03/31', 'yyyy/mm/dd'))
   --{?End Date})
   and tdl.detail_type in ( '1','10')
   group by elink.episode_id) MC_Epis on base_pats.episode_id = MC_Epis.episode_id

-- This is the total quantity and charge totals of Modalities codes in the episode performed within the report date range. Each transaction (tdl.tx_id) is also evaluated to ensure it has the LBP codes identified in the CODES temp table
left outer join
    (select elink.episode_id, sum(tdl.procedure_quantity) as proc_qty, sum(tdl.amount) as proc_amt
   from episode_link elink
   left outer join clarity_tdl_tran tdl on elink.pat_enc_csn_id = tdl.pat_enc_csn_id
   join (   select distinct vdx.tx_id, arpb.pat_enc_csn_id
              from EPICADM.arpb_transactions arpb
              join epicadm.tx_diag vdx on arpb.tx_id = vdx.tx_id
              inner join edg_current_icd9 icd9 on vdx.dx_id = icd9.dx_id
         ) txcodes on tdl.pat_enc_csn_id = txcodes.pat_enc_csn_id and tdl.tx_id = txcodes.tx_id
   where tdl.cpt_code in ('97014','97035','97033','97032','G0283')    
   --and (tdl.orig_service_date between {?Start Date} AND {?End Date})
   and (tdl.orig_service_date between TO_DATE ('2015/01/01', 'yyyy/mm/dd') and
TO_DATE('2015/03/31', 'yyyy/mm/dd'))
   --{?Start Date} AND {?End Date})
   and tdl.detail_type in ( '1','10')
   group by elink.episode_id) MD on base_pats.episode_id = MD.episode_id

-- This is the total quantity and charge totals of all Modalities codes in the episode performed between episode start date and report end date. Each transaction (tdl.tx_id) is also evaluated to ensure it has the LBP codes identified in the CODES temp table
left outer join
    (select elink.episode_id, sum(tdl.amount) as proc_amt
   from episode epi
   left outer join episode_link elink on epi.episode_id = elink.episode_id
   left outer join clarity_tdl_tran tdl on elink.pat_enc_csn_id = tdl.pat_enc_csn_id
   join (   select distinct vdx.tx_id, arpb.pat_enc_csn_id
              from EPICADM.arpb_transactions arpb
              join epicadm.tx_diag vdx on arpb.tx_id = vdx.tx_id
              inner join edg_current_icd9 icd9 on vdx.dx_id = icd9.dx_id
         ) txcodes on tdl.pat_enc_csn_id = txcodes.pat_enc_csn_id and tdl.tx_id = txcodes.tx_id
   where tdl.cpt_code in ('97014','97035','97033','97032','G0283') 
   --and (tdl.orig_service_date <= {?End Date})
   and (tdl.orig_service_date between epi.start_date AND TO_DATE('2015/03/31', 'yyyy/mm/dd'))
   --{?End Date})
   and tdl.detail_type in ( '1','10')
   group by elink.episode_id) MD_Epis on base_pats.episode_id = MD_Epis.episode_id
  
-- This is the total quantity and charge totals of Traction codes in the episode performed within the report date range. Each transaction (tdl.tx_id) is also evaluated to ensure it has the LBP codes identified in the CODES temp table  
left outer join
    (select elink.episode_id, sum(tdl.procedure_quantity) as proc_qty, sum(tdl.amount) as proc_amt
   from episode_link elink
   left outer join clarity_tdl_tran tdl on elink.pat_enc_csn_id = tdl.pat_enc_csn_id
   join (   select distinct vdx.tx_id, arpb.pat_enc_csn_id
              from EPICADM.arpb_transactions arpb
              join epicadm.tx_diag vdx on arpb.tx_id = vdx.tx_id
              inner join edg_current_icd9 icd9 on vdx.dx_id = icd9.dx_id
         ) txcodes on tdl.pat_enc_csn_id = txcodes.pat_enc_csn_id and tdl.tx_id = txcodes.tx_id
   where tdl.cpt_code = '97012'     
   --and (tdl.orig_service_date between {?Start Date} AND {?End Date})
   and (tdl.orig_service_date between TO_DATE ('2015/01/01', 'yyyy/mm/dd') and
TO_DATE('2015/03/31', 'yyyy/mm/dd'))
   --{?Start Date} AND {?End Date})
   and tdl.detail_type in ( '1','10')
   group by elink.episode_id) Trac on base_pats.episode_id = Trac.episode_id

 

-- This is the total quantity and charge totals of all Traction codes in the episode performed between episode start date and report end date. Each transaction (tdl.tx_id) is also evaluated to ensure it has the LBP codes identified in the CODES temp table
--******When this is commented out the report runs in seconds.****************
--left outer join
    --(select elink.episode_id, sum(tdl.amount) as proc_amt
   --from episode epi
   --left outer join episode_link elink on epi.episode_id = elink.episode_id
   --left outer join clarity_tdl_tran tdl on elink.pat_enc_csn_id = tdl.pat_enc_csn_id
   --join (   select distinct vdx.tx_id, arpb.pat_enc_csn_id
              --from EPICADM.arpb_transactions arpb
              --join epicadm.tx_diag vdx on arpb.tx_id = vdx.tx_id
              --inner join edg_current_icd9 icd9 on vdx.dx_id = icd9.dx_id
         --) txcodes on tdl.pat_enc_csn_id = txcodes.pat_enc_csn_id and tdl.tx_id = txcodes.tx_id
   --where tdl.cpt_code = '97012'   
   --and (tdl.orig_service_date <= {?End Date})
   --and (tdl.orig_service_date between epi.start_date AND TO_DATE('2015/03/31', 'yyyy/mm/dd'))
   --{?End Date})
  -- and tdl.detail_type in ( '1','10')
   --group by elink.episode_id) Trac_Epis on base_pats.episode_id = Trac_Epis.episode_id
  
-- This is the total quantity and charge totals of all Evaluate codes in the episode performed between episode start date and report end date. Each transaction (tdl.tx_id) is also evaluated to ensure it has the LBP codes identified in the CODES temp table  
left outer join
    (select elink.episode_id, sum(tdl.amount) as proc_amt
   from episode epi
   left outer join episode_link elink on epi.episode_id = elink.episode_id
   left outer join clarity_tdl_tran tdl on elink.pat_enc_csn_id = tdl.pat_enc_csn_id
   join (   select distinct vdx.tx_id, arpb.pat_enc_csn_id
              from EPICADM.arpb_transactions arpb
              join epicadm.tx_diag vdx on arpb.tx_id = vdx.tx_id
              inner join edg_current_icd9 icd9 on vdx.dx_id = icd9.dx_id
         ) txcodes on tdl.pat_enc_csn_id = txcodes.pat_enc_csn_id and tdl.tx_id = txcodes.tx_id
   where tdl.cpt_code in ('97001','97002')  
   --and (tdl.orig_service_date <= {?End Date})
   and (tdl.orig_service_date between epi.start_date and TO_DATE('2015/03/31', 'yyyy/mm/dd'))
   --AND {?End Date})
   and tdl.detail_type in ( '1','10')
   group by elink.episode_id) Eval_Epis on base_pats.episode_id = Eval_Epis.episode_id

left outer join
  (select elink4.episode_id, min(edg4.code) as scode
      from episode_link elink4
      left outer join pat_enc penc4 on elink4.pat_enc_csn_id = penc4.pat_enc_csn_id
      left outer join pat_enc_dx pdx4 on penc4.pat_enc_csn_id = pdx4.pat_enc_csn_id
      left outer join edg_current_icd9 edg4 on pdx4.dx_id = edg4.dx_id
      where (penc4.contact_date between  TO_DATE ('2015/01/01', 'yyyy/mm/dd') and
TO_DATE('2015/03/31', 'yyyy/mm/dd'))
      --{?Start Date} AND {?End Date})
      and edg4.code = 'V45.89'       --ICD9 code for surgical process
      group by elink4.episode_id)  surg_code on base_pats.episode_id = surg_code.episode_id



Viewing all articles
Browse latest Browse all 8451

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>