Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WITH CNT_1 AS (SELECT mv_stat_lvl_1_declarations.employee_id,
- 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,
- 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,
- 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,
- 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
- FROM mv_stat_lvl_0_employees
- JOIN mv_stat_lvl_1_declarations ON mv_stat_lvl_1_declarations.employee_id = mv_stat_lvl_0_employees.id
- AND mv_stat_lvl_1_declarations.legal_entity_id = mv_stat_lvl_0_employees.legal_entity_id
- JOIN mv_stat_lvl_1_reception_conditions ON mv_stat_lvl_1_reception_conditions.patient_id = mv_stat_lvl_1_declarations.patient_id
- AND mv_stat_lvl_1_reception_conditions.legal_entity_id = mv_stat_lvl_1_declarations.legal_entity_id
- AND (mv_stat_lvl_1_reception_conditions.code IN ('K85', 'K86', 'K87', 'T90', 'R96') OR mv_stat_lvl_1_reception_conditions.code LIKE 'P%')
- WHERE mv_stat_lvl_0_employees.legal_entity_id = 3228 AND mv_stat_lvl_0_employees.employee_type_ref_value = 'DOCTOR'
- GROUP BY mv_stat_lvl_1_declarations.employee_id),
- CNT_2 AS (SELECT mv_stat_lvl_0_receptions.employee_id,
- COUNT(DISTINCT mv_stat_lvl_0_receptions.patient_id) AS reception_counter
- FROM mv_stat_lvl_0_employees
- JOIN mv_stat_lvl_0_receptions ON mv_stat_lvl_0_receptions.legal_entity_id = mv_stat_lvl_0_employees.legal_entity_id
- AND mv_stat_lvl_0_receptions.employee_id = mv_stat_lvl_0_employees.id
- AND mv_stat_lvl_0_receptions.asserted_date BETWEEN '2022-01-01 00:00:00' AND '2024-09-15 23:59:59.999999'
- WHERE mv_stat_lvl_0_employees.legal_entity_id = 3228 AND mv_stat_lvl_0_employees.employee_type_ref_value = 'DOCTOR'
- GROUP BY mv_stat_lvl_0_receptions.employee_id),
- CNT_3 AS (SELECT mv_stat_lvl_1_declarations.employee_id,
- COUNT(DISTINCT mv_stat_lvl_1_patients.id) AS smoking_counter
- FROM mv_stat_lvl_0_employees
- JOIN mv_stat_lvl_1_declarations ON mv_stat_lvl_1_declarations.legal_entity_id = mv_stat_lvl_0_employees.legal_entity_id
- AND mv_stat_lvl_1_declarations.employee_id = mv_stat_lvl_0_employees.id
- JOIN mv_stat_lvl_1_patients ON mv_stat_lvl_1_patients.is_smoking = true
- AND mv_stat_lvl_1_declarations.patient_id = mv_stat_lvl_1_patients.id
- AND mv_stat_lvl_1_patients.age BETWEEN 18 AND 69
- WHERE mv_stat_lvl_0_employees.legal_entity_id = 3228 AND mv_stat_lvl_0_employees.employee_type_ref_value = 'DOCTOR'
- GROUP BY mv_stat_lvl_1_declarations.employee_id),
- CNT_4 AS (SELECT mv_stat_lvl_2_imt_indices.employee_id,
- COUNT(DISTINCT mv_stat_lvl_2_imt_indices.patient_id) AS overweight_counter
- FROM mv_stat_lvl_0_employees
- 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
- AND mv_stat_lvl_2_imt_indices.employee_id = mv_stat_lvl_0_employees.id
- AND mv_stat_lvl_2_imt_indices.imt_index > 25
- AND mv_stat_lvl_2_imt_indices.asserted_date BETWEEN '2022-01-01 00:00:00' AND '2024-09-15 23:59:59.999999'
- 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
- WHERE mv_stat_lvl_0_employees.legal_entity_id = 3228
- AND mv_stat_lvl_0_employees.employee_type_ref_value = 'DOCTOR'
- GROUP BY mv_stat_lvl_2_imt_indices.employee_id),
- CNT_5 AS (SELECT mv_stat_lvl_1_declarations.employee_id,
- COUNT(DISTINCT mv_stat_lvl_1_declarations.patient_id) AS total_counter
- FROM mv_stat_lvl_0_employees
- JOIN mv_stat_lvl_1_declarations ON mv_stat_lvl_1_declarations.legal_entity_id = mv_stat_lvl_0_employees.legal_entity_id
- AND mv_stat_lvl_1_declarations.employee_id = mv_stat_lvl_0_employees.id
- WHERE mv_stat_lvl_0_employees.legal_entity_id = 3228
- AND mv_stat_lvl_0_employees.employee_type_ref_value = 'DOCTOR'
- GROUP BY mv_stat_lvl_1_declarations.employee_id)
- SELECT
- mv_stat_lvl_0_employees.id,
- mv_stat_lvl_0_employees.full_name,
- COALESCE(MAX(diabetes_counter),0) as diabetes_counter,
- COALESCE(MAX(hypertension_counter),0) as hypertension_counter,
- COALESCE(MAX(asthma_counter),0) as asthma_counter,
- COALESCE(MAX(derangement_counter),0) as derangement_counter,
- COALESCE(MAX(smoking_counter),0) as smoking_counter,
- COALESCE(MAX(reception_counter),0) as reception_counter,
- COALESCE(MAX(overweight_counter),0) as overweight_counter,
- COALESCE(MAX(total_counter),0) as total_counter
- FROM mv_stat_lvl_0_employees
- LEFT JOIN CNT_1 ON CNT_1.employee_id = mv_stat_lvl_0_employees.id
- LEFT JOIN CNT_2 ON CNT_2.employee_id = mv_stat_lvl_0_employees.id
- LEFT JOIN CNT_3 ON CNT_3.employee_id = mv_stat_lvl_0_employees.id
- LEFT JOIN CNT_4 ON CNT_4.employee_id = mv_stat_lvl_0_employees.id
- LEFT JOIN CNT_5 ON CNT_5.employee_id = mv_stat_lvl_0_employees.id
- WHERE mv_stat_lvl_0_employees.employee_type_ref_value = 'DOCTOR'
- AND mv_stat_lvl_0_employees.legal_entity_id = 3228
- 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