简体   繁体   中英

Inserting parameters to SQL query in Oracle SQL

In the following query between date time columns are repeated in multiple places and I need to replace them with two variables named start_date and end_date I tried multiple methods and had no luck. Please answer with a runnable query if you can. Thanks in advance.

WITH encounter
     AS (SELECT patient_pomr_id AS encounter_number,
                patient_id      AS umrn,
                doctor_id,
                doctor_name
         FROM   eh_pomr.ehpom_patient_pomr
         WHERE  created_on BETWEEN timestamp '2022-08-01 00:00:00' AND
                                   timestamp '2022-08-30 00:00:00'),
     chief_complain
     AS (SELECT chief_complain,
                patient_pomr_id
         FROM   eh_pomr.ehpom_chief_complain),
     admission
     AS (SELECT admitted_date,
                patient_id,
                ADMISSION_ID,
                admission_type AS encounter_type,
                patient_pomr_id,
                hospital_id,
                clinic_name
         FROM   ad_request.admlm_admission
         WHERE  direct_admission IS NULL
                AND is_from_er != 1
                AND created_date BETWEEN timestamp '2022-08-01 00:00:00' AND
                                         timestamp '2022-08-30 00:00:00'),
     ip_create_admission
     AS (SELECT patientpomr,
                dbms_lob.Substr(admitting_diagnosis, 2000, 1) diagnosis
         FROM   eh_ip.ehip_create_admission
         WHERE  created_on BETWEEN timestamp '2022-08-01 00:00:00' AND
                                   timestamp '2022-08-30 00:00:00'),
     discharge
     AS (SELECT CASE
                  WHEN dischargevia = 1 THEN 'Private Vehicle'
                  WHEN dischargevia = 2 THEN 'Ambulatory'
                  WHEN dischargevia = 3 THEN 'Other'
                  ELSE ' Unknown'
                END                  AS dischargevia,
                pomrid,
                modifiedon           AS discharge_date,
                conditionondischarge AS discharge_speciality
         FROM   eh_ndischarge.ehipd_dischargedetails
         WHERE  isactive = 1),
     death
     AS (SELECT dbms_lob.Substr(underlying_cause, 2000, 1) cause_of_death,
                patientpomr
         FROM   eh_ip.ehip_death_detail),
     empi
     AS (SELECT id_number,
                mrn
         FROM   rf_empi.emred_patients),
     vitals
     AS (SELECT PR.id,
                PR.patient_pomr_id,
                FS.field_code,
                FS.value
         FROM   eh_commmon.ehcom_patient_record PR
                left join eh_commmon.ehcom_flow_sheet_data FS
                       ON PR.id = FS.patient_record_id
         WHERE  PR.flow_sheet_code = 'vitals'
                AND FS.time_stamp BETWEEN timestamp '2022-08-01 00:00:00' AND
                                          timestamp '2022-08-30 00:00:00'),
     leaves
     AS (SELECT requesting_days,
       visit_id,
       ADM.PATIENT_POMR_ID 
FROM   ad_request.admlm_med_leave_final_print FP
       left join ad_request.admlm_medical_leave ML
              ON FP.request_id = ML.request_id
       LEFT JOIN AD_REQUEST.ADMLM_ADMISSION ADM
                ON ML.VISIT_ID = ADM.ADMISSION_ID 
WHERE  FP.leave_status = 5
       AND ML.created_date BETWEEN timestamp '2022-08-01 00:00:00' AND
                                   timestamp '2022-08-30 00:00:00' 
       AND ML.REQUESTING_DAYS IS NOT NULL)
SELECT DISTINCT encounter.encounter_number,
                admission.encounter_type,
                empi.id_number         AS Patient_National_ID,
                admission.patient_id   AS umrn,
                admission.admitted_date,
                admission.hospital_id,
                admission.clinic_name  AS admission_speciality,
                chief_complain.chief_complain,
                leaves.requesting_days AS Duration_of_leave,
                encounter.doctor_id,
                encounter.doctor_name,
                ip_create_admission.diagnosis,
                discharge.dischargevia,
                discharge.discharge_date,
                discharge_speciality,
                admission.clinic_name  AS clinic,
                death.cause_of_death
--       VITALS.field_code,
--       VITALS.value
FROM   admission
       left join empi
              ON admission.patient_id = empi.mrn
       left join encounter
              ON admission.patient_pomr_id = encounter.encounter_number
       left join ip_create_admission
              ON admission.patient_pomr_id = ip_create_admission.patientpomr
       --admission_request_numbrer with adt
       left join discharge
              ON admission.patient_pomr_id = discharge.pomrid
       left join death
              ON admission.patient_pomr_id = death.patientpomr
       left join chief_complain
              ON admission.patient_pomr_id = chief_complain.patient_pomr_id
       left join leaves
              ON admission.patient_pomr_id = leaves.PATIENT_POMR_ID

I tried adding with begin and end tags with declare key words but had no luck. Also is there a special way to insert variable using in to keyword when we need to insert it for between?

This is from MSSQL, you can try converting this through OracleSQL

@dateFrom datetime = null,
@dateTo datetime = null,

DATEADD(D, 0, DATEDIFF(D, 0, @DateFrom))
AND DATEADD(D, 0, DATEDIFF(D, 0, @DateTo)) 

Include yet another CTE (I'm calling it dates ) which is then cross-joined in another CTEs which utilize these values. Something like this:

WITH 
dates (start_date, end_date)                          --> this is new CTE
     AS (SELECT timestamp '2022-08-01 00:00:00', 
                timestamp '2022-08-30 00:00:00'
         FROM dual),
encounter
     AS (SELECT patient_pomr_id AS encounter_number,
                patient_id      AS umrn,
                doctor_id,
                doctor_name
         FROM   eh_pomr.ehpom_patient_pomr
                CROSS JOIN dates d                                --> it is used here
         WHERE  created_on BETWEEN d.start_date AND d.end_date),  --> like this
chief_complain 
     AS ..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM