Quantcast
Channel: SCN : Unanswered Discussions - SAP Crystal Reports
Viewing all articles
Browse latest Browse all 4814

Command Object Running slowly problem

$
0
0

The following SQL is running very slowly.  Any suggestions?

 

WITH
--temp table codes  that holds the IAM LBP(lower back pain) ICD9 DX IDs and diagnosis codes. The codes will be used throughout the report and this temp table makes the report run faster since the query is not executed multiple times
codes as
(
select  edg9.dx_id, edg9.code
from edg_current_icd9 edg9
where 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%'
),

base_pats As
(
Select Distinct Penc.Pat_Enc_Csn_Id,  Penc.Pat_Id, Penc.Contact_Date, Codes.Code, Epi.Episode_Id,Vf.Mrn,
Vf.Pat_Name,  Floor((Penc.Contact_Date - Pat.Birth_Date)/365.25) As Age, Zep_Type.Name As Epi_Type, Zep_Status.Name As Epi_Status,
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_Hsp 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 Codes Codes On Pdx.Dx_Id = Codes.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 Patient Pat On Penc.Pat_Id = Pat.Pat_Id
   left outer join epicadm.v_fv_mrn vf on Pat.pat_id = vf.pat_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.Department_Id In (450024.00, 460023.00, 54513.00, 55113.00,55411,55706,54313,54407)

And Penc.Adt_Pat_Class_C= '115'--Therapies Series
And Penc.Adt_Patient_Stat_C = 6 --Hospital Outpatient Visit
AND PENC.ADMIT_CONF_STAT_C = 4 --Admission Complete
And Penc.Contact_Date Between To_Date ('2014/08/01', 'yyyy/mm/dd')
And To_Date ('2014/08/30', 'yyyy/mm/dd')
)
--Main Query
Select Distinct
Base_Pats.Pat_Enc_Csn_Id,  Base_Pats.Pat_Id, Base_Pats.Contact_Date, Base_Pats.Code, Base_Pats.Episode_Id,Base_Pats.Mrn,
Base_Pats.Pat_Name, Base_Pats.Age, Base_Pats.Epi_Type, Base_Pats.Epi_Status, Base_Pats.Episode_Name, Base_Pats.Epi_Start,
Base_Pats.Epi_End,
Hot_packs.hp_count as Hot_Packs_Ordered,
Eval_Epis.proc_amt as Eval_Codes_Amt,
Trac.proc_qty as  Traction, Trac.proc_amt as  Traction_Amt, Trac_Epis.proc_amt as Traction_Amt_Epis,
Ac.Proc_Qty As  Active_Care, Ac.Proc_Amt As  Active_Care_Amt, Ac_Epis.Proc_Amt As Active_Care_Amt_Epis,
Md.Proc_Qty As  Modalities, Md.Proc_Amt As  Modalities_Amt, Md_Epis.Proc_Amt As Modalities_Amt_Epis,
Mc.Proc_Qty As  Manual_Care, Mc.Proc_Amt As  Manual_Care_Amt, Mc_Epis.Proc_Amt As Manual_Care_Amt_Epis,

-- 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,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,

surg_code.scode as surgical_code

From Base_Pats Base_Pats

-- This is the total quantity and charge totals of Active Care codes in the episode performed within the report date range. Each transaction (hsp.tx_id) is also evaluated to ensure it has the LBP codes identified in the CODES temp table
left outer join
    (select elink3.episode_id, sum(hsp.quantity) as proc_qty, sum(hsp.tx_amount) as proc_amt
   from episode_link elink3
   left outer join hsp_transactions hsp on elink3.pat_enc_csn_id = hsp.pat_enc_csn_id
   where hsp.cpt_code in ('97116','97112','97110','97530','97537','97535')   -- CPT codes classified as Active Care by IAM
   and (hsp.service_date  Between To_Date ('2014/08/01', 'yyyy/mm/dd')
And To_Date ('2014/08/30', 'yyyy/mm/dd'))
   and hsp.TX_TYPE_HA_C  in (1,3,4)
   group by elink3.episode_id) AC on base_pats.episode_id = AC.episode_id
  
left outer join
  (select elink4.episode_id, min(edg4.code) as scode
      From Episode_Link Elink4
      left outer join pat_enc_hsp 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 ('2014/08/01', 'yyyy/mm/dd')
And To_Date ('2014/08/30', 'yyyy/mm/dd'))
      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

-- 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 (hsp.tx_id) is also evaluated to ensure it has the LBP codes identified in the CODES temp table
left outer join
    (select elink3.episode_id, sum(hsp.tx_amount) as proc_amt
   from episode epi
   left outer join episode_link elink3 on epi.episode_id = elink3.episode_id
   left outer join hsp_transactions hsp on elink3.pat_enc_csn_id = hsp.pat_enc_csn_id
   where hsp.cpt_code in ('97116','97112','97110','97530','97537','97535')    -- CPT codes classified as Active Care by IAM
   and (hsp.service_date  between epi.start_date AND To_Date ('2014/08/30', 'yyyy/mm/dd'))
   and hsp.TX_TYPE_HA_C  in (1,3,4)
   group by elink3.episode_id) AC_Epis on base_pats.episode_id = AC_Epis.episode_id

-- Modalities
left outer join
    (select elink3.episode_id, sum(hsp.quantity) as proc_qty, sum(hsp.tx_amount) as proc_amt
   from episode_link elink3
   Left Outer Join Hsp_Transactions Hsp On Elink3.Pat_Enc_Csn_Id = Hsp.Pat_Enc_Csn_Id
   where hsp.cpt_code in ('97014','97035','97033','97032')    
   And (Hsp.Service_Date  Between To_Date ('2014/08/01', 'yyyy/mm/dd')
And To_Date ('2014/08/30', 'yyyy/mm/dd'))
    And Hsp.Tx_Type_Ha_C In (1,3,4)  
Group By Elink3.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 (hsp.tx_id) is also evaluated to ensure it has the LBP codes identified in the CODES temp table
left outer join
    (select elink3.episode_id, sum(hsp.tx_amount) as proc_amt
   from episode epi
   left outer join episode_link elink3 on epi.episode_id = elink3.episode_id
   Left Outer Join Hsp_Transactions  Hsp On Elink3.Pat_Enc_Csn_Id = Hsp.Pat_Enc_Csn_Id
   Where Hsp.Cpt_Code In ('97014','97035','97033','97032') 
   And (Hsp.Service_Date Between Epi.Start_Date And To_Date ('2014/08/30', 'yyyy/mm/dd'))
   and hsp.TX_TYPE_HA_C in (1,3,4)     
Group By Elink3.Episode_Id) Md_Epis On Base_Pats.Episode_Id = Md_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 (hsp.tx_id) is also evaluated to ensure it has the LBP codes identified in the CODES temp table  
left outer join
    (select elink3.episode_id, sum(hsp.quantity) as proc_qty, sum(hsp.tx_amount) as proc_amt
   from episode_link elink3
   left outer join hsp_transactions hsp on elink3.pat_enc_csn_id = hsp.pat_enc_csn_id
   where hsp.cpt_code = '97140'       -- CPT code classified as Manual Care by IAM
   and (hsp.service_date between  To_Date  ('2014/08/01', 'yyyy/mm/dd')
And To_Date ('2014/08/30', 'yyyy/mm/dd'))
   and hsp.TX_TYPE_HA_C   in (1,3,4)  
group by elink3.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 (hsp.tx_id) is also evaluated to ensure it has the LBP codes identified in the CODES temp table
left outer join
    (select elink3.episode_id, sum(hsp.tx_amount) as proc_amt
   from episode epi
   left outer join episode_link elink3 on epi.episode_id = elink3.episode_id
   left outer join hsp_transactions hsp on elink3.pat_enc_csn_id = hsp.pat_enc_csn_id
   where hsp.cpt_code = '97140'  
   and (hsp.service_date between epi.start_date AND To_Date ('2014/08/30', 'yyyy/mm/dd'))
    and hsp.TX_TYPE_HA_C  in ( 1,3,4)  
   Group By Elink3.Episode_Id) Mc_Epis On Base_Pats.Episode_Id = Mc_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 (hsp.tx_id) is also evaluated to ensure it has the LBP codes identified in the CODES temp table  
left outer join
    (select elink3.episode_id, sum(hsp.quantity) as proc_qty, sum(hsp.tx_amount) as proc_amt
   from episode_link elink3
   left outer join hsp_transactions  hsp on elink3.pat_enc_csn_id = hsp.pat_enc_csn_id
   where hsp.cpt_code = '97012'     
   and (hsp.service_date Between To_Date ('2014/08/01', 'yyyy/mm/dd')
   And To_Date ('2014/08/30', 'yyyy/mm/dd'))
   and hsp.TX_TYPE_HA_C  in (1,3,4)     
   group by elink3.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 (hsp.tx_id) is also evaluated to ensure it has the LBP codes identified in the CODES temp table
left outer join
    (select elink3.episode_id, sum(hsp.tx_amount) as proc_amt
   from episode epi
   left outer join episode_link elink3 on epi.episode_id = elink3.episode_id
   left outer join hsp_transactions hsp on elink3.pat_enc_csn_id = hsp.pat_enc_csn_id
   where hsp.cpt_code = '97012'   
   and (hsp.service_date between epi.start_date AND To_Date ('2014/08/30', 'yyyy/mm/dd'))
   and hsp.TX_TYPE_HA_C  in (1,3,4)     
   Group By Elink3.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 (hsp.tx_id) is also evaluated to ensure it has the LBP codes identified in the CODES temp table  
left outer join
    (select elink3.episode_id, sum(hsp.tx_amount) as proc_amt
   from episode epi
   left outer join episode_link elink3 on epi.episode_id = elink3.episode_id
   left outer join  hsp_transactions hsp on elink3.pat_enc_csn_id = hsp.pat_enc_csn_id
   where hsp.cpt_code in ('97001','97002')  
   and (hsp.service_date between epi.start_date AND To_Date ('2014/08/30', 'yyyy/mm/dd'))
    and hsp.TX_TYPE_HA_C  in ( 1,3,4)     
   Group By Elink3.Episode_Id) Eval_Epis On Base_Pats.Episode_Id = Eval_Epis.Episode_Id

-- Sub query that counts the number of Hot Packs ordered in the date range
left outer join
   (
      select distinct elink1.episode_id, count(elink1.pat_enc_csn_id) as HP_count
      from episode_link elink1
      left outer join order_proc ord   on elink1.pat_enc_csn_id = ord.pat_enc_csn_id
      Where (Ord.Ordering_Date Between To_Date ('2014/08/01', 'yyyy/mm/dd')
And To_Date ('2014/08/30', 'yyyy/mm/dd'))
      and ord.proc_code = '97010'
      group by elink1.episode_id
   ) Hot_Packs On Base_Pats.Episode_Id = Hot_Packs.Episode_Id
  
left outer join
  (select elink4.episode_id, min(edg4.code) as scode
      From Episode_Link Elink4
      left outer join pat_enc_hsp 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 ('2014/08/01', 'yyyy/mm/dd')
And To_Date ('2014/08/30', 'yyyy/mm/dd'))
      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


--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 distinct 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 ('2014/08/30', 'yyyy/mm/dd'))
                         Left Outer Join Pat_Enc_Hsp Penc2 On Vflo_Oswestry_2.Pat_Enc_Csn_Id = Penc2.Pat_Enc_Csn_Id
                         left outer join clarity_ser ser2 on penc2.admission_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 distinct 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 ('2014/08/30', 'yyyy/mm/dd'))
                         Left Outer Join Pat_Enc_Hsp Penc3 On Vflo_Oswestry_3.Pat_Enc_Csn_Id = Penc3.Pat_Enc_Csn_Id
                         left outer join clarity_ser ser3 on penc3.admission_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 distinct 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 ('2014/08/30', 'yyyy/mm/dd'))
                         Left Outer Join Pat_Enc_Hsp Penc2 On Vflo_Keele_Sub.Pat_Enc_Csn_Id = Penc2.Pat_Enc_Csn_Id
                         left outer join clarity_ser ser2 on penc2.admission_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 distinct 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 ('2014/08/30', 'yyyy/mm/dd'))
                         Left Outer Join Pat_Enc_Hsp Penc2 On Vflo_Keele_Sub.Pat_Enc_Csn_Id = Penc2.Pat_Enc_Csn_Id
                         left outer join clarity_ser ser2 on penc2.admission_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 distinct 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 ('2014/08/30', 'yyyy/mm/dd'))
                         left outer join pat_enc_hsp 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 distinct 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 ('2014/08/30', 'yyyy/mm/dd'))
                         left outer join pat_enc_hsp 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 distinct 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_hsp 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 ('2014/08/30', 'yyyy/mm/dd'))
                     ) 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 distinct 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_hsp 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 ('2014/08/30', 'yyyy/mm/dd'))
                     ) Latest_Pam_Score on base_pats.pat_id = Latest_Pam_Score.pat_id and Latest_Pam_Score.enc_rank = 1



Viewing all articles
Browse latest Browse all 4814

Trending Articles



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