Advertisement
temaon_lieto

HUGE counters SQL

Feb 16th, 2024
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. WITH CNT_1 AS (SELECT mv_stat_lvl_1_declarations.employee_id,
  2.                 COUNT(DISTINCT mv_stat_lvl_1_reception_conditions.patient_id) filter (WHERE mv_stat_lvl_1_reception_conditions.code IN ('K85', 'K86', 'K87') AND mv_stat_lvl_1_reception_conditions.asserted_date BETWEEN '2022-01-01 00:00:00' AND '2024-02-15 23:59:59.999999' ) AS hypertension_counter,
  3.                 COUNT(DISTINCT mv_stat_lvl_1_reception_conditions.patient_id) filter (WHERE mv_stat_lvl_1_reception_conditions.code IN ('T90') AND mv_stat_lvl_1_reception_conditions.asserted_date BETWEEN '2022-01-01 00:00:00' AND '2024-02-15 23:59:59.999999') AS diabetes_counter,
  4.                 COUNT(DISTINCT mv_stat_lvl_1_reception_conditions.patient_id) filter (WHERE mv_stat_lvl_1_reception_conditions.code IN ('R96') AND mv_stat_lvl_1_reception_conditions.asserted_date BETWEEN '2022-01-01 00:00:00' AND '2024-02-15 23:59:59.999999') AS asthma_counter,
  5.                 COUNT(DISTINCT mv_stat_lvl_1_reception_conditions.patient_id) filter (WHERE mv_stat_lvl_1_reception_conditions.code LIKE 'P%' AND mv_stat_lvl_1_reception_conditions.asserted_date BETWEEN '2022-01-01 00:00:00' AND '2024-02-15 23:59:59.999999') AS derangement_counter
  6.             FROM mv_stat_lvl_0_employees
  7.             JOIN mv_stat_lvl_1_declarations ON mv_stat_lvl_1_declarations.employee_id = mv_stat_lvl_0_employees.id
  8.                                                    AND mv_stat_lvl_1_declarations.legal_entity_id = mv_stat_lvl_0_employees.legal_entity_id
  9.             JOIN mv_stat_lvl_1_reception_conditions ON mv_stat_lvl_1_reception_conditions.patient_id = mv_stat_lvl_1_declarations.patient_id
  10.                                                            AND mv_stat_lvl_1_reception_conditions.legal_entity_id = mv_stat_lvl_1_declarations.legal_entity_id
  11.                                                            AND (mv_stat_lvl_1_reception_conditions.code IN ('K85', 'K86', 'K87', 'T90', 'R96') OR mv_stat_lvl_1_reception_conditions.code LIKE 'P%')
  12.             WHERE mv_stat_lvl_0_employees.legal_entity_id = 3228 AND mv_stat_lvl_0_employees.employee_type_ref_value = 'DOCTOR'
  13.             GROUP BY mv_stat_lvl_1_declarations.employee_id),
  14.     CNT_2 AS (SELECT mv_stat_lvl_0_receptions.employee_id,
  15.                 COUNT(DISTINCT mv_stat_lvl_0_receptions.patient_id) AS reception_counter
  16.             FROM mv_stat_lvl_0_employees
  17.             JOIN mv_stat_lvl_0_receptions ON mv_stat_lvl_0_receptions.legal_entity_id = mv_stat_lvl_0_employees.legal_entity_id
  18.                                                  AND mv_stat_lvl_0_receptions.employee_id = mv_stat_lvl_0_employees.id
  19.                                                  AND mv_stat_lvl_0_receptions.asserted_date BETWEEN '2022-01-01 00:00:00' AND '2024-09-15 23:59:59.999999'
  20.  
  21.             WHERE mv_stat_lvl_0_employees.legal_entity_id = 3228 AND mv_stat_lvl_0_employees.employee_type_ref_value = 'DOCTOR'
  22.             GROUP BY mv_stat_lvl_0_receptions.employee_id),
  23.     CNT_3 AS (SELECT mv_stat_lvl_1_declarations.employee_id,
  24.                 COUNT(DISTINCT mv_stat_lvl_1_patients.id) AS smoking_counter
  25.             FROM mv_stat_lvl_0_employees
  26.             JOIN mv_stat_lvl_1_declarations ON mv_stat_lvl_1_declarations.legal_entity_id = mv_stat_lvl_0_employees.legal_entity_id
  27.                                                    AND mv_stat_lvl_1_declarations.employee_id = mv_stat_lvl_0_employees.id
  28.             JOIN mv_stat_lvl_1_patients ON mv_stat_lvl_1_patients.is_smoking = true
  29.                                                AND mv_stat_lvl_1_declarations.patient_id = mv_stat_lvl_1_patients.id
  30.                                                AND mv_stat_lvl_1_patients.age BETWEEN 18 AND 69
  31.             WHERE mv_stat_lvl_0_employees.legal_entity_id = 3228 AND mv_stat_lvl_0_employees.employee_type_ref_value = 'DOCTOR'
  32.             GROUP BY mv_stat_lvl_1_declarations.employee_id),
  33.     CNT_4 AS (SELECT mv_stat_lvl_2_imt_indices.employee_id,
  34.                 COUNT(DISTINCT mv_stat_lvl_2_imt_indices.patient_id) AS overweight_counter
  35.             FROM mv_stat_lvl_0_employees
  36.             JOIN mv_stat_lvl_2_imt_indices ON mv_stat_lvl_2_imt_indices.legal_entity_id = mv_stat_lvl_0_employees.legal_entity_id
  37.                                                   AND mv_stat_lvl_2_imt_indices.employee_id = mv_stat_lvl_0_employees.id
  38.                                                   AND mv_stat_lvl_2_imt_indices.imt_index > 25
  39.                                                   AND mv_stat_lvl_2_imt_indices.asserted_date BETWEEN '2022-01-01 00:00:00' AND '2024-09-15 23:59:59.999999'
  40.             JOIN mv_stat_lvl_1_patients ON mv_stat_lvl_2_imt_indices.patient_id = mv_stat_lvl_1_patients.id AND mv_stat_lvl_1_patients.age BETWEEN 18 AND 69
  41.             WHERE mv_stat_lvl_0_employees.legal_entity_id = 3228
  42.             AND mv_stat_lvl_0_employees.employee_type_ref_value = 'DOCTOR'
  43.             GROUP BY mv_stat_lvl_2_imt_indices.employee_id),
  44.  
  45.     CNT_5 AS (SELECT mv_stat_lvl_1_declarations.employee_id,
  46.                 COUNT(DISTINCT mv_stat_lvl_1_declarations.patient_id) AS total_counter
  47.             FROM mv_stat_lvl_0_employees
  48.             JOIN mv_stat_lvl_1_declarations ON mv_stat_lvl_1_declarations.legal_entity_id = mv_stat_lvl_0_employees.legal_entity_id
  49.                                                    AND mv_stat_lvl_1_declarations.employee_id = mv_stat_lvl_0_employees.id
  50.             WHERE mv_stat_lvl_0_employees.legal_entity_id = 3228
  51.             AND mv_stat_lvl_0_employees.employee_type_ref_value = 'DOCTOR'
  52.             GROUP BY mv_stat_lvl_1_declarations.employee_id)
  53.  
  54. SELECT
  55.     mv_stat_lvl_0_employees.id,
  56.     mv_stat_lvl_0_employees.full_name,
  57.     COALESCE(MAX(diabetes_counter),0) as diabetes_counter,
  58.     COALESCE(MAX(hypertension_counter),0) as hypertension_counter,
  59.     COALESCE(MAX(asthma_counter),0) as asthma_counter,
  60.     COALESCE(MAX(derangement_counter),0) as derangement_counter,
  61.     COALESCE(MAX(smoking_counter),0) as smoking_counter,
  62.     COALESCE(MAX(reception_counter),0) as reception_counter,
  63.     COALESCE(MAX(overweight_counter),0) as overweight_counter,
  64.     COALESCE(MAX(total_counter),0) as total_counter
  65. FROM mv_stat_lvl_0_employees
  66.     LEFT JOIN CNT_1 ON CNT_1.employee_id = mv_stat_lvl_0_employees.id
  67.     LEFT JOIN CNT_2 ON CNT_2.employee_id = mv_stat_lvl_0_employees.id
  68.     LEFT JOIN CNT_3 ON CNT_3.employee_id = mv_stat_lvl_0_employees.id
  69.     LEFT JOIN CNT_4 ON CNT_4.employee_id = mv_stat_lvl_0_employees.id
  70.     LEFT JOIN CNT_5 ON CNT_5.employee_id = mv_stat_lvl_0_employees.id
  71. WHERE mv_stat_lvl_0_employees.employee_type_ref_value = 'DOCTOR'
  72.     AND mv_stat_lvl_0_employees.legal_entity_id = 3228
  73. GROUP BY mv_stat_lvl_0_employees.id, mv_stat_lvl_0_employees.full_name
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement