Advertisement
temaon_lieto

issued-sql

Jul 17th, 2025 (edited)
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. INSERT INTO cascade_lvl02_discharge_encounters(
  2.     id,
  3.     history_number,
  4.     order_via_history_number,
  5.     line_number,
  6.     major,
  7.     minor,
  8.     encounter_id,
  9.     discharge_disposition_code,
  10.     asserted_date,
  11.     period_start_date,
  12.     main_icd10_code,
  13.     birth_date,
  14.     bed_days_counter,
  15.     legal_entity_id,
  16.     patient_id,
  17.     patient_age,
  18.     patient_age_month,
  19.     patient_weight,
  20.     is_plurality_pregnancies
  21. )
  22. WITH patients AS (SELECT id,
  23.                          birth_date,
  24.                          legal_entity_id
  25.                   FROM cascade_lvl01_patients),
  26.     bed_days_encounters AS (
  27.         SELECT
  28.             bde.history_number,
  29.             toDateTime(MIN(bde.period_start), 'Europe/Kyiv') AS period_start,
  30.             toDateTime(MAX(bde.asserted_date), 'Europe/Kyiv') AS asserted_date,
  31.         CASE
  32.             WHEN period_start IS NULL THEN 0
  33.             ELSE
  34.                 CASE
  35.                     WHEN asserted_date IS NULL OR asserted_date < period_start THEN 0
  36.                     ELSE
  37.                         CASE dateDiff('day', period_start, asserted_date)
  38.                             WHEN 0 THEN 1
  39.                             ELSE dateDiff('day', period_start, asserted_date)
  40.                             END
  41.                     END
  42.             END as bed_days_counter,
  43.             bde.patient_id,
  44.             argMin(bde.encounter_id, bde.order_via_history_number) AS encounter_id,
  45.             argMin(bde.episode_id, bde.order_via_history_number) AS episode_id,
  46.             argMin(bde.discharge_disposition_code, bde.order_via_history_number) AS discharge_disposition_code,
  47.             MIN(bde.order_via_history_number) AS order_via_history_number,
  48.             any(bde.legal_entity_id) AS legal_entity_id
  49.         FROM cascade_lvl00_bed_days_encounters bde
  50.         WHERE bde.legal_entity_id = 3387
  51.         GROUP BY bde.patient_id, bde.history_number
  52.     )
  53. SELECT
  54.     first_value(e.episode_id)                                                    AS id,
  55.     e.history_number                                                              AS history_number,
  56.     e.order_via_history_number                                                    AS order_via_history_number,
  57.     nullIf(dt3220i10m.line_number, '')                                            AS line_number,
  58.     CAST(splitByString('.', CAST(dt3220i10m.line_number AS String))[1] AS UInt16) AS major,
  59.     CAST(
  60.             splitByString('.', CAST(dt3220i10m.line_number AS String))[2] AS
  61.         Nullable(UInt16))                                                     AS minor,
  62.     e.encounter_id                                                                AS encounter_id,
  63.     e.discharge_disposition_code                                                  as discharge_disposition_code,
  64.     toDate(e.asserted_date)                                                       AS asserted_date,
  65.     e.period_start                                                                AS period_start_date,
  66.     CASE
  67.         WHEN discharge_disposition_code = 'death'
  68.             THEN coalesce(nullIf(fic.icd10_code, ''), nullIf(ic.icd10_code, ''))
  69.         ELSE nullIf(ic.icd10_code, '')
  70.         END                                                                               AS main_icd10_code,
  71.     p.birth_date                                                                  AS birth_date,
  72.     e.bed_days_counter                                                            AS bed_days_counter,
  73.     e.legal_entity_id                                                             AS legal_entity_id,
  74.     e.patient_id                                                                  AS patient_id,
  75.     CASE
  76.         WHEN birth_date IS NULL OR toDate(birth_date) > toDate(now()) THEN 199
  77.         ELSE age('year', toDate(birth_date), toDate(COALESCE(period_start_date, asserted_date)))
  78.         END AS patient_age,
  79.     CASE
  80.         WHEN birth_date IS NULL OR toDate(birth_date) > toDate(now()) THEN 2388
  81.         ELSE age('month', toDate(birth_date), toDate(COALESCE(period_start_date, asserted_date)))
  82.         END AS patient_age_month,
  83.     iwo.value                                                                     AS patient_weight,
  84.     iwo.is_plurality_pregnancies                                                  AS is_plurality_pregnancies
  85. FROM bed_days_encounters e
  86.          LEFT JOIN cascade_lvl00_form066_icd10_codes fic ON fic.episode_id = e.episode_id
  87.          LEFT JOIN cascade_lvl00_primary_icd10_codes ic ON ic.id = e.encounter_id
  88.          LEFT JOIN cascade_lvl00_infant_weight_observations iwo
  89.                    ON iwo.episode_id = e.episode_id
  90.          LEFT JOIN dict_table3220_icd10_mappers dt3220i10m
  91.                    ON ic.icd10_code = dt3220i10m.code OR fic.icd10_code = dt3220i10m.code
  92.          JOIN patients p ON e.patient_id = p.id
  93. WHERE main_icd10_code IS NOT NULL AND line_number IS NOT NULL
  94. GROUP BY
  95.     e.history_number,
  96.     e.order_via_history_number,
  97.     dt3220i10m.line_number,
  98.     major,
  99.     minor,
  100.     e.encounter_id,
  101.     e.discharge_disposition_code,
  102.     asserted_date,
  103.     e.period_start,
  104.     main_icd10_code,
  105.     p.birth_date,
  106.     e.bed_days_counter,
  107.     e.legal_entity_id,
  108.     e.patient_id,
  109.     patient_age,
  110.     patient_age_month,
  111.     iwo.value,
  112.     iwo.is_plurality_pregnancies
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement