Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- NOW
- -- +---------------+----------------+--------------------+-----------------------+---------------------+-----------+-------------------------+----------------------------+-----------------------+-----------------------+---------------------------+------------------------------+----------------------------+------------------+--------------------------------+-----------------------------------+------------------------------+
- -- |legal_entity_id|diabetes_counter|hypertension_counter|prostate_cancer_counter|breast_cancer_counter|hiv_counter|colorectal_cancer_counter|visits_from_40_to_64_counter|visits_upper_65_counter|target_diabetes_counter|target_hypertension_counter|target_prostate_cancer_counter|target_breast_cancer_counter|target_hiv_counter|target_colorectal_cancer_counter|target_visits_from_40_to_64_counter|target_visits_upper_65_counter|
- -- +---------------+----------------+--------------------+-----------------------+---------------------+-----------+-------------------------+----------------------------+-----------------------+-----------------------+---------------------------+------------------------------+----------------------------+------------------+--------------------------------+-----------------------------------+------------------------------+
- -- |8185 |518 |450 |210 |189 |0 |368 |51 |0 |918 |851 |420 |290 |1952 |633 |793 |294 |
- -- +---------------+----------------+--------------------+-----------------------+---------------------+-----------+-------------------------+----------------------------+-----------------------+-----------------------+---------------------------+------------------------------+----------------------------+------------------+--------------------------------+-----------------------------------+------------------------------+
- WITH "items" AS (SELECT mv_stat_lvl_2_joined_items.id,
- mv_stat_lvl_2_joined_items.asserted_date,
- mv_stat_lvl_2_joined_items.code,
- mv_stat_lvl_2_joined_items.item_type,
- mv_stat_lvl_2_joined_items.status::varchar,
- mv_stat_lvl_2_joined_items.title,
- mv_stat_lvl_2_joined_items.employee_id,
- mv_stat_lvl_2_joined_items.legal_entity_id,
- mv_stat_lvl_2_joined_items.patient_id
- FROM "mv_stat_lvl_2_joined_items"
- WHERE "mv_stat_lvl_2_joined_items"."asserted_date" BETWEEN '2024-01-01 00:00:00' AND '2024-04-11 23:59:59.999999'
- AND "mv_stat_lvl_2_joined_items"."legal_entity_id" = 8185
- AND "mv_stat_lvl_2_joined_items"."code" IN
- ('T34006', 'T34011', 'T34013', 'T34024', 'T34001', 'T34004', 'T34005', 'T34023', 'T34025',
- 'T34038', 'U67002', 'Y34011', 'Y34003', 'Х41941', 'X41973', '59300-00', '55070-00', '55076-00',
- 'D36003', 'D67006', '32084-00', '32090-00', '32084-02', '32090-02', 'B33006', 'B33012',
- '14743-9')),
- "main_counters" AS (SELECT COALESCE(COUNT(DISTINCT mv_stat_lvl_1_declarations.id), 0) AS counter,
- mv_stat_lvl_1_declarations.patient_id AS patient_id,
- CASE
- WHEN (("mv_stat_lvl_1_patients"."gender" = 1 AND DATE_PART('YEAR',
- AGE('2024-04-11 23:59:59.999999', mv_stat_lvl_1_patients.birth_date)) BETWEEN 50 AND 999) OR
- ("mv_stat_lvl_1_patients"."gender" = 0 AND DATE_PART('YEAR',
- AGE('2024-04-11 23:59:59.999999', mv_stat_lvl_1_patients.birth_date)) BETWEEN 40 AND 999))
- THEN
- CASE
- WHEN (COUNT(DISTINCT items.code) filter (WHERE "items"."code" IN
- ('T34006', 'T34011',
- 'T34013', 'T34024',
- 'T34001', 'T34004'))) > 0
- THEN 'done'
- ELSE 'need_done'
- END
- ELSE
- 'no_need_done'
- END hypertension_state,
- CASE
- WHEN (DATE_PART('YEAR',
- AGE('2024-04-11 23:59:59.999999', mv_stat_lvl_1_patients.birth_date)) BETWEEN 45 AND 999)
- THEN
- CASE
- WHEN (COUNT(DISTINCT items.code)
- filter (WHERE "items"."code" IN ('T34005', 'T34023', 'T34025', 'T34038', '14743-9'))) >
- 0 THEN 'done'
- ELSE 'need_done'
- END
- ELSE
- 'no_need_done'
- END AS diabetes_state,
- CASE
- WHEN ("mv_stat_lvl_1_patients"."gender" = 0 AND (DATE_PART('YEAR',
- AGE('2024-04-11 23:59:59.999999', mv_stat_lvl_1_patients.birth_date)) BETWEEN 40 AND 999))
- THEN
- CASE
- WHEN (COUNT(DISTINCT items.code)
- filter (WHERE "items"."code" IN ('U67002', 'Y34011', 'Y34003'))) > 0
- THEN 'done'
- ELSE 'need_done'
- END
- ELSE
- 'no_need_done'
- END AS prostate_cancer_state,
- CASE
- WHEN ("mv_stat_lvl_1_patients"."gender" = 1 AND (DATE_PART('YEAR',
- AGE('2024-04-11 23:59:59.999999', mv_stat_lvl_1_patients.birth_date)) BETWEEN 50 AND 69))
- THEN
- CASE
- WHEN (COUNT(DISTINCT items.code) filter (WHERE "items"."code" IN
- ('Х41941', 'X41973',
- '59300-00', '55070-00',
- '55076-00'))) > 0
- THEN 'done'
- ELSE 'need_done'
- END
- ELSE
- 'no_need_done'
- END AS breast_cancer_state,
- CASE
- WHEN (DATE_PART('YEAR',
- AGE('2024-04-11 23:59:59.999999', mv_stat_lvl_1_patients.birth_date)) BETWEEN 50 AND 75)
- THEN
- CASE
- WHEN (COUNT(DISTINCT items.code) filter (WHERE "items"."code" IN
- ('D36003', 'D67006',
- '32084-00', '32090-00',
- '32084-02', '32090-02'))) >
- 0 THEN 'done'
- ELSE 'need_done'
- END
- ELSE
- 'no_need_done'
- END AS colorectal_cancer_state,
- CASE
- WHEN (DATE_PART('YEAR',
- AGE('2024-04-11 23:59:59.999999', mv_stat_lvl_1_patients.birth_date)) BETWEEN 14 AND 999)
- THEN
- CASE
- WHEN (COUNT(DISTINCT items.code)
- filter (WHERE "items"."code" IN ('B33006', 'B33012'))) > 0 THEN 'done'
- ELSE 'need_done'
- END
- ELSE
- 'no_need_done'
- END AS hiv_state
- FROM "mv_stat_lvl_1_declarations"
- JOIN mv_stat_lvl_1_patients ON mv_stat_lvl_1_patients.legal_entity_id =
- mv_stat_lvl_1_declarations.legal_entity_id AND
- mv_stat_lvl_1_patients.id =
- mv_stat_lvl_1_declarations.patient_id
- LEFT JOIN items
- ON items.legal_entity_id = mv_stat_lvl_1_declarations.legal_entity_id AND
- items.patient_id = mv_stat_lvl_1_declarations.patient_id
- WHERE "mv_stat_lvl_1_declarations"."legal_entity_id" = 8185
- GROUP BY mv_stat_lvl_1_declarations.patient_id, mv_stat_lvl_1_patients.birth_date,
- mv_stat_lvl_1_patients.gender),
- "diagnoses" AS (SELECT mv_stat_lvl_1_reception_conditions.patient_id, mv_stat_lvl_1_reception_conditions.code
- FROM "mv_stat_lvl_1_reception_conditions"
- WHERE "mv_stat_lvl_1_reception_conditions"."asserted_date" BETWEEN '2024-01-01 00:00:00' AND '2024-04-11 23:59:59.999999'
- AND "mv_stat_lvl_1_reception_conditions"."legal_entity_id" = 8185),
- "observations" AS (SELECT mv_stat_lvl_1_ehr_observations.patient_id, mv_stat_lvl_1_ehr_observations.code
- FROM "mv_stat_lvl_1_ehr_observations"
- WHERE "mv_stat_lvl_1_ehr_observations"."asserted_date" BETWEEN '2024-01-01 00:00:00' AND '2024-04-11 23:59:59.999999'
- AND "mv_stat_lvl_1_ehr_observations"."legal_entity_id" = 8185
- AND "mv_stat_lvl_1_ehr_observations"."code" IN
- ('39156-5', '14743-9', '8462-4', '8480-6', '56086-2', '4548-4')),
- "diagnosis_cnt" AS (SELECT diagnoses.patient_id,
- COUNT(DISTINCT diagnoses.code) AS diagnoses_c1_64_counter,
- COUNT(DISTINCT diagnoses.code)
- filter (WHERE "diagnoses"."code" IN ('A98')) AS diagnoses_a98_counter,
- COUNT(DISTINCT diagnoses.code)
- filter (WHERE "diagnoses"."code" IN ('K86', 'K87')) AS diagnoses_c1_65_counter,
- COUNT(DISTINCT diagnoses.code)
- filter (WHERE "diagnoses"."code" IN ('K76', 'K74')) AS diagnoses_c2_65_counter,
- COUNT(DISTINCT diagnoses.code)
- filter (WHERE "diagnoses"."code" IN ('T89', 'T90')) AS diagnoses_c3_65_counter
- FROM diagnoses
- GROUP BY "diagnoses"."patient_id"
- HAVING (COUNT(DISTINCT diagnoses.code) > 0)),
- "observations_cnt" AS (SELECT observations.patient_id,
- COUNT(DISTINCT observations.code) filter (WHERE "observations"."code" IN
- ('39156-5', '14743-9', '8462-4',
- '8480-6',
- '56086-2')) AS observations_c1_64_counter,
- COUNT(DISTINCT observations.code) filter (WHERE "observations"."code" IN
- ('39156-5', '14743-9', '8462-4',
- '8480-6',
- '56086-2')) AS observations_c1_65_counter,
- COUNT(DISTINCT observations.code) filter (WHERE "observations"."code" IN
- ('39156-5', '14743-9', '8462-4',
- '8480-6', '56086-2',
- '4548-4')) AS observations_c2_65_counter
- FROM observations
- GROUP BY "observations"."patient_id"
- HAVING (COUNT(DISTINCT observations.code) > 0)),
- "reasons_cnt" AS (SELECT mv_stat_lvl_1_ehr_reasons.patient_id,
- COUNT(DISTINCT mv_stat_lvl_1_ehr_reasons.code) AS a98_counter
- FROM "mv_stat_lvl_1_ehr_reasons"
- WHERE "mv_stat_lvl_1_ehr_reasons"."code" = 'A98'
- AND "mv_stat_lvl_1_ehr_reasons"."asserted_date" BETWEEN '2024-01-01 00:00:00' AND '2024-04-11 23:59:59.999999'
- AND "mv_stat_lvl_1_ehr_reasons"."legal_entity_id" = 8185
- GROUP BY "mv_stat_lvl_1_ehr_reasons"."patient_id"
- HAVING (COUNT(DISTINCT mv_stat_lvl_1_ehr_reasons.code) > 0)),
- "reports_cnt" AS (SELECT mv_stat_lvl_0_ehr_diagnostic_reports.patient_id,
- COUNT(DISTINCT mv_stat_lvl_0_ehr_diagnostic_reports.code) AS counter
- FROM "mv_stat_lvl_0_ehr_diagnostic_reports"
- WHERE "mv_stat_lvl_0_ehr_diagnostic_reports"."code" IN
- ('T34001', 'T34006', 'T34024', 'T34011', 'T34013')
- AND "mv_stat_lvl_0_ehr_diagnostic_reports"."asserted_date" BETWEEN '2024-01-01 00:00:00' AND '2024-04-11 23:59:59.999999'
- AND "mv_stat_lvl_0_ehr_diagnostic_reports"."legal_entity_id" = 8185
- GROUP BY "mv_stat_lvl_0_ehr_diagnostic_reports"."patient_id"
- HAVING (COUNT(DISTINCT mv_stat_lvl_0_ehr_diagnostic_reports.code) > 0)),
- "actions_cnt" AS (SELECT mv_stat_lvl_1_ehr_actions.patient_id,
- COUNT(DISTINCT mv_stat_lvl_1_ehr_actions.code) AS counter
- FROM "mv_stat_lvl_1_ehr_actions"
- WHERE "mv_stat_lvl_1_ehr_actions"."code" IN ('K45', 'D45', 'T45')
- AND "mv_stat_lvl_1_ehr_actions"."asserted_date" BETWEEN '2024-01-01 00:00:00' AND '2024-04-11 23:59:59.999999'
- AND "mv_stat_lvl_1_ehr_actions"."legal_entity_id" = 8185
- GROUP BY "mv_stat_lvl_1_ehr_actions"."patient_id"
- HAVING (COUNT(DISTINCT mv_stat_lvl_1_ehr_actions.code) > 0)),
- "groups_counter" AS (SELECT mv_stat_lvl_1_patients.id AS patient_id,
- CASE
- WHEN (DATE_PART('YEAR',
- AGE('2024-04-11 23:59:59.999999', mv_stat_lvl_1_patients.birth_date)) BETWEEN 40 AND 64)
- THEN
- CASE
- WHEN (diagnosis_cnt.diagnoses_a98_counter > 0 OR reasons_cnt.a98_counter > 0) AND
- reports_cnt.counter > 0 AND
- observations_cnt.observations_c1_64_counter > 0 THEN 'done'
- ELSE 'need_done'
- END
- ELSE 'no_need_done'
- END AS visits_from_40_to_64_state,
- CASE
- WHEN (DATE_PART('YEAR',
- AGE('2024-04-11 23:59:59.999999', mv_stat_lvl_1_patients.birth_date)) BETWEEN 65 AND 999)
- THEN
- CASE
- WHEN (diagnosis_cnt.diagnoses_a98_counter > 0 OR reasons_cnt.a98_counter > 0) AND
- (((diagnosis_cnt.diagnoses_c1_65_counter > 0 OR
- diagnosis_cnt.diagnoses_c2_65_counter > 0) AND
- observations_cnt.observations_c1_65_counter > 0) OR
- (diagnosis_cnt.diagnoses_c3_65_counter > 0 AND
- observations_cnt.observations_c2_65_counter > 0)) THEN 'done'
- ELSE 'need_done'
- END
- ELSE 'no_need_done'
- END AS visits_upper_65_state
- FROM "mv_stat_lvl_1_patients"
- LEFT JOIN observations_cnt
- ON mv_stat_lvl_1_patients.id = observations_cnt.patient_id AND
- (observations_cnt.observations_c1_64_counter >= 5 OR
- observations_cnt.observations_c1_65_counter >= 5 OR
- observations_cnt.observations_c2_65_counter >= 6)
- LEFT JOIN diagnosis_cnt ON mv_stat_lvl_1_patients.id = diagnosis_cnt.patient_id AND
- (diagnosis_cnt.diagnoses_c1_64_counter > 0 OR
- diagnosis_cnt.diagnoses_c1_65_counter > 0 OR
- diagnosis_cnt.diagnoses_c2_65_counter > 0 OR
- diagnosis_cnt.diagnoses_c3_65_counter > 0)
- LEFT JOIN reasons_cnt ON reasons_cnt.patient_id = mv_stat_lvl_1_patients.id
- LEFT JOIN reports_cnt ON reports_cnt.patient_id = mv_stat_lvl_1_patients.id
- LEFT JOIN actions_cnt ON actions_cnt.patient_id = mv_stat_lvl_1_patients.id
- WHERE "mv_stat_lvl_1_patients"."legal_entity_id" = 8185
- GROUP BY mv_stat_lvl_1_patients.id, mv_stat_lvl_1_patients.age,
- mv_stat_lvl_1_patients.birth_date, visits_from_40_to_64_state, visits_upper_65_state)
- SELECT mv_stat_lvl_1_declarations.legal_entity_id,
- COUNT(main_counters.diabetes_state) filter (WHERE diabetes_state = 'done') as diabetes_counter,
- COUNT(main_counters.hypertension_state)
- filter (WHERE hypertension_state = 'done') as hypertension_counter,
- COUNT(main_counters.prostate_cancer_state)
- filter (WHERE prostate_cancer_state = 'done') as prostate_cancer_counter,
- COUNT(main_counters.breast_cancer_state)
- filter (WHERE breast_cancer_state = 'done') as breast_cancer_counter,
- COUNT(main_counters.hiv_state) filter (WHERE hiv_state = 'done') as hiv_counter,
- COUNT(main_counters.colorectal_cancer_state)
- filter (WHERE colorectal_cancer_state = 'done') as colorectal_cancer_counter,
- COUNT(groups_counter.visits_from_40_to_64_state)
- filter (WHERE visits_from_40_to_64_state = 'done') as visits_from_40_to_64_counter,
- COUNT(groups_counter.visits_upper_65_state)
- filter (WHERE visits_upper_65_state = 'done') as visits_upper_65_counter,
- COUNT(main_counters.diabetes_state)
- filter (WHERE diabetes_state IN ('done', 'need_done')) as target_diabetes_counter,
- COUNT(main_counters.hypertension_state)
- filter (WHERE hypertension_state IN ('done', 'need_done')) as target_hypertension_counter,
- COUNT(main_counters.prostate_cancer_state)
- filter (WHERE prostate_cancer_state IN ('done', 'need_done')) as target_prostate_cancer_counter,
- COUNT(main_counters.breast_cancer_state)
- filter (WHERE breast_cancer_state IN ('done', 'need_done')) as target_breast_cancer_counter,
- COUNT(main_counters.hiv_state)
- filter (WHERE hiv_state IN ('done', 'need_done')) as target_hiv_counter,
- COUNT(main_counters.colorectal_cancer_state)
- filter (WHERE colorectal_cancer_state IN ('done', 'need_done')) as target_colorectal_cancer_counter,
- COUNT(groups_counter.visits_from_40_to_64_state)
- filter (WHERE visits_from_40_to_64_state IN ('done', 'need_done')) as target_visits_from_40_to_64_counter,
- COUNT(groups_counter.visits_upper_65_state)
- filter (WHERE visits_upper_65_state IN ('done', 'need_done')) as target_visits_upper_65_counter
- FROM "mv_stat_lvl_1_declarations"
- LEFT JOIN main_counters ON mv_stat_lvl_1_declarations.patient_id = main_counters.patient_id
- LEFT JOIN groups_counter ON mv_stat_lvl_1_declarations.patient_id = groups_counter.patient_id
- WHERE "mv_stat_lvl_1_declarations"."legal_entity_id" = 8185
- GROUP BY "mv_stat_lvl_1_declarations"."legal_entity_id"
- LIMIT 1;
- -- WAS
- -- +---------------+----------------+--------------------+-----------------------+---------------------+-----------+-------------------------+----------------------------+-----------------------+-----------------------+---------------------------+------------------------------+----------------------------+------------------+--------------------------------+-----------------------------------+------------------------------+
- -- |legal_entity_id|diabetes_counter|hypertension_counter|prostate_cancer_counter|breast_cancer_counter|hiv_counter|colorectal_cancer_counter|visits_from_40_to_64_counter|visits_upper_65_counter|target_diabetes_counter|target_hypertension_counter|target_prostate_cancer_counter|target_breast_cancer_counter|target_hiv_counter|target_colorectal_cancer_counter|target_visits_from_40_to_64_counter|target_visits_upper_65_counter|
- -- +---------------+----------------+--------------------+-----------------------+---------------------+-----------+-------------------------+----------------------------+-----------------------+-----------------------+---------------------------+------------------------------+----------------------------+------------------+--------------------------------+-----------------------------------+------------------------------+
- -- |8185 |518 |450 |210 |189 |0 |368 |51 |0 |918 |851 |420 |290 |1952 |633 |793 |294 |
- -- +---------------+----------------+--------------------+-----------------------+---------------------+-----------+-------------------------+----------------------------+-----------------------+-----------------------+---------------------------+------------------------------+----------------------------+------------------+--------------------------------+-----------------------------------+------------------------------+
- WITH "items" AS (SELECT "mv_stat_lvl_0_ehr_service_requests".*
- FROM ((SELECT "mv_stat_lvl_0_ehr_service_requests".*
- FROM ((SELECT mv_stat_lvl_0_ehr_service_requests.id,
- mv_stat_lvl_0_ehr_service_requests.asserted_date,
- mv_stat_lvl_0_ehr_service_requests.code,
- mv_stat_lvl_0_ehr_service_requests.item_type,
- mv_stat_lvl_0_ehr_service_requests.status::varchar,
- mv_stat_lvl_0_ehr_service_requests.title,
- mv_stat_lvl_0_ehr_service_requests.employee_id,
- mv_stat_lvl_0_ehr_service_requests.legal_entity_id,
- mv_stat_lvl_0_ehr_service_requests.patient_id
- FROM "mv_stat_lvl_0_ehr_service_requests"
- WHERE "mv_stat_lvl_0_ehr_service_requests"."asserted_date" BETWEEN '2024-01-01 00:00:00' AND '2024-04-11 23:59:59.999999'
- AND "mv_stat_lvl_0_ehr_service_requests"."legal_entity_id" = 8185
- AND "mv_stat_lvl_0_ehr_service_requests"."code" IN
- ('T34006', 'T34011', 'T34013', 'T34024', 'T34001', 'T34004', 'T34005', 'T34023',
- 'T34025', 'T34038', 'U67002', 'Y34011', 'Y34003', 'Х41941', 'X41973', '59300-00',
- '55070-00', '55076-00', 'D36003', 'D67006', '32084-00', '32090-00', '32084-02',
- '32090-02', 'B33006', 'B33012', '14743-9'))
- UNION
- (SELECT mv_stat_lvl_0_ehr_diagnostic_reports.id,
- mv_stat_lvl_0_ehr_diagnostic_reports.asserted_date,
- mv_stat_lvl_0_ehr_diagnostic_reports.code,
- mv_stat_lvl_0_ehr_diagnostic_reports.item_type,
- mv_stat_lvl_0_ehr_diagnostic_reports.status::varchar,
- mv_stat_lvl_0_ehr_diagnostic_reports.title,
- mv_stat_lvl_0_ehr_diagnostic_reports.employee_id,
- mv_stat_lvl_0_ehr_diagnostic_reports.legal_entity_id,
- mv_stat_lvl_0_ehr_diagnostic_reports.patient_id
- FROM "mv_stat_lvl_0_ehr_diagnostic_reports"
- WHERE "mv_stat_lvl_0_ehr_diagnostic_reports"."asserted_date" BETWEEN '2024-01-01 00:00:00' AND '2024-04-11 23:59:59.999999'
- AND "mv_stat_lvl_0_ehr_diagnostic_reports"."legal_entity_id" = 8185
- AND "mv_stat_lvl_0_ehr_diagnostic_reports"."code" IN
- ('T34006', 'T34011', 'T34013', 'T34024', 'T34001', 'T34004', 'T34005', 'T34023',
- 'T34025', 'T34038', 'U67002', 'Y34011', 'Y34003', 'Х41941', 'X41973', '59300-00',
- '55070-00', '55076-00', 'D36003', 'D67006', '32084-00', '32090-00', '32084-02',
- '32090-02', 'B33006', 'B33012', '14743-9'))) "mv_stat_lvl_0_ehr_service_requests")
- UNION
- (SELECT mv_stat_lvl_1_ehr_observations.id,
- mv_stat_lvl_1_ehr_observations.asserted_date,
- mv_stat_lvl_1_ehr_observations.code,
- 1 as item_type,
- 'final'::varchar as status,
- mv_stat_lvl_1_ehr_observations.title,
- mv_stat_lvl_1_ehr_observations.employee_id,
- mv_stat_lvl_1_ehr_observations.legal_entity_id,
- mv_stat_lvl_1_ehr_observations.patient_id
- FROM "mv_stat_lvl_1_ehr_observations"
- WHERE "mv_stat_lvl_1_ehr_observations"."asserted_date" BETWEEN '2024-01-01 00:00:00' AND '2024-04-11 23:59:59.999999'
- AND "mv_stat_lvl_1_ehr_observations"."legal_entity_id" = 8185
- AND "mv_stat_lvl_1_ehr_observations"."code" IN
- ('T34006', 'T34011', 'T34013', 'T34024', 'T34001', 'T34004', 'T34005', 'T34023', 'T34025',
- 'T34038', 'U67002', 'Y34011', 'Y34003', 'Х41941', 'X41973', '59300-00', '55070-00',
- '55076-00', 'D36003', 'D67006', '32084-00', '32090-00', '32084-02', '32090-02', 'B33006',
- 'B33012', '14743-9'))) "mv_stat_lvl_0_ehr_service_requests"),
- "main_counters" AS (SELECT COALESCE(COUNT(DISTINCT mv_stat_lvl_1_declarations.id), 0) AS counter,
- mv_stat_lvl_1_declarations.patient_id AS patient_id,
- CASE
- WHEN (("mv_stat_lvl_1_patients"."gender" = 1 AND DATE_PART('YEAR',
- AGE('2024-04-11 23:59:59.999999', mv_stat_lvl_1_patients.birth_date)) BETWEEN 50 AND 999) OR
- ("mv_stat_lvl_1_patients"."gender" = 0 AND DATE_PART('YEAR',
- AGE('2024-04-11 23:59:59.999999', mv_stat_lvl_1_patients.birth_date)) BETWEEN 40 AND 999))
- THEN
- CASE
- WHEN (COUNT(DISTINCT items.code) filter (WHERE "items"."code" IN
- ('T34006', 'T34011',
- 'T34013', 'T34024',
- 'T34001', 'T34004'))) > 0
- THEN 'done'
- ELSE 'need_done'
- END
- ELSE
- 'no_need_done'
- END hypertension_state,
- CASE
- WHEN (DATE_PART('YEAR',
- AGE('2024-04-11 23:59:59.999999', mv_stat_lvl_1_patients.birth_date)) BETWEEN 45 AND 999)
- THEN
- CASE
- WHEN (COUNT(DISTINCT items.code)
- filter (WHERE "items"."code" IN ('T34005', 'T34023', 'T34025', 'T34038', '14743-9'))) >
- 0 THEN 'done'
- ELSE 'need_done'
- END
- ELSE
- 'no_need_done'
- END AS diabetes_state,
- CASE
- WHEN ("mv_stat_lvl_1_patients"."gender" = 0 AND (DATE_PART('YEAR',
- AGE('2024-04-11 23:59:59.999999', mv_stat_lvl_1_patients.birth_date)) BETWEEN 40 AND 999))
- THEN
- CASE
- WHEN (COUNT(DISTINCT items.code)
- filter (WHERE "items"."code" IN ('U67002', 'Y34011', 'Y34003'))) > 0
- THEN 'done'
- ELSE 'need_done'
- END
- ELSE
- 'no_need_done'
- END AS prostate_cancer_state,
- CASE
- WHEN ("mv_stat_lvl_1_patients"."gender" = 1 AND (DATE_PART('YEAR',
- AGE('2024-04-11 23:59:59.999999', mv_stat_lvl_1_patients.birth_date)) BETWEEN 50 AND 69))
- THEN
- CASE
- WHEN (COUNT(DISTINCT items.code) filter (WHERE "items"."code" IN
- ('Х41941', 'X41973',
- '59300-00', '55070-00',
- '55076-00'))) > 0
- THEN 'done'
- ELSE 'need_done'
- END
- ELSE
- 'no_need_done'
- END AS breast_cancer_state,
- CASE
- WHEN (DATE_PART('YEAR',
- AGE('2024-04-11 23:59:59.999999', mv_stat_lvl_1_patients.birth_date)) BETWEEN 50 AND 75)
- THEN
- CASE
- WHEN (COUNT(DISTINCT items.code) filter (WHERE "items"."code" IN
- ('D36003', 'D67006',
- '32084-00', '32090-00',
- '32084-02', '32090-02'))) >
- 0 THEN 'done'
- ELSE 'need_done'
- END
- ELSE
- 'no_need_done'
- END AS colorectal_cancer_state,
- CASE
- WHEN (DATE_PART('YEAR',
- AGE('2024-04-11 23:59:59.999999', mv_stat_lvl_1_patients.birth_date)) BETWEEN 14 AND 999)
- THEN
- CASE
- WHEN (COUNT(DISTINCT items.code)
- filter (WHERE "items"."code" IN ('B33006', 'B33012'))) > 0 THEN 'done'
- ELSE 'need_done'
- END
- ELSE
- 'no_need_done'
- END AS hiv_state
- FROM "mv_stat_lvl_1_declarations"
- JOIN mv_stat_lvl_1_patients ON mv_stat_lvl_1_patients.legal_entity_id =
- mv_stat_lvl_1_declarations.legal_entity_id AND
- mv_stat_lvl_1_patients.id =
- mv_stat_lvl_1_declarations.patient_id
- LEFT JOIN items
- ON items.legal_entity_id = mv_stat_lvl_1_declarations.legal_entity_id AND
- items.patient_id = mv_stat_lvl_1_declarations.patient_id
- WHERE "mv_stat_lvl_1_declarations"."legal_entity_id" = 8185
- GROUP BY mv_stat_lvl_1_declarations.patient_id, mv_stat_lvl_1_patients.birth_date,
- mv_stat_lvl_1_patients.gender),
- "diagnoses" AS (SELECT mv_stat_lvl_1_reception_conditions.patient_id, mv_stat_lvl_1_reception_conditions.code
- FROM "mv_stat_lvl_1_reception_conditions"
- WHERE "mv_stat_lvl_1_reception_conditions"."asserted_date" BETWEEN '2024-01-01 00:00:00' AND '2024-04-11 23:59:59.999999'
- AND "mv_stat_lvl_1_reception_conditions"."legal_entity_id" = 8185),
- "observations" AS (SELECT mv_stat_lvl_1_ehr_observations.patient_id, mv_stat_lvl_1_ehr_observations.code
- FROM "mv_stat_lvl_1_ehr_observations"
- WHERE "mv_stat_lvl_1_ehr_observations"."asserted_date" BETWEEN '2024-01-01 00:00:00' AND '2024-04-11 23:59:59.999999'
- AND "mv_stat_lvl_1_ehr_observations"."legal_entity_id" = 8185
- AND "mv_stat_lvl_1_ehr_observations"."code" IN
- ('39156-5', '14743-9', '8462-4', '8480-6', '56086-2', '4548-4')),
- "diagnosis_cnt" AS (SELECT diagnoses.patient_id,
- COUNT(DISTINCT diagnoses.code) AS diagnoses_c1_64_counter,
- COUNT(DISTINCT diagnoses.code)
- filter (WHERE "diagnoses"."code" IN ('A98')) AS diagnoses_a98_counter,
- COUNT(DISTINCT diagnoses.code)
- filter (WHERE "diagnoses"."code" IN ('K86', 'K87')) AS diagnoses_c1_65_counter,
- COUNT(DISTINCT diagnoses.code)
- filter (WHERE "diagnoses"."code" IN ('K76', 'K74')) AS diagnoses_c2_65_counter,
- COUNT(DISTINCT diagnoses.code)
- filter (WHERE "diagnoses"."code" IN ('T89', 'T90')) AS diagnoses_c3_65_counter
- FROM diagnoses
- GROUP BY "diagnoses"."patient_id"
- HAVING (COUNT(DISTINCT diagnoses.code) > 0)),
- "observations_cnt" AS (SELECT observations.patient_id,
- COUNT(DISTINCT observations.code) filter (WHERE "observations"."code" IN
- ('39156-5', '14743-9', '8462-4',
- '8480-6',
- '56086-2')) AS observations_c1_64_counter,
- COUNT(DISTINCT observations.code) filter (WHERE "observations"."code" IN
- ('39156-5', '14743-9', '8462-4',
- '8480-6',
- '56086-2')) AS observations_c1_65_counter,
- COUNT(DISTINCT observations.code) filter (WHERE "observations"."code" IN
- ('39156-5', '14743-9', '8462-4',
- '8480-6', '56086-2',
- '4548-4')) AS observations_c2_65_counter
- FROM observations
- GROUP BY "observations"."patient_id"
- HAVING (COUNT(DISTINCT observations.code) > 0)),
- "reasons_cnt" AS (SELECT mv_stat_lvl_1_ehr_reasons.patient_id,
- COUNT(DISTINCT mv_stat_lvl_1_ehr_reasons.code) AS a98_counter
- FROM "mv_stat_lvl_1_ehr_reasons"
- WHERE "mv_stat_lvl_1_ehr_reasons"."code" = 'A98'
- AND "mv_stat_lvl_1_ehr_reasons"."asserted_date" BETWEEN '2024-01-01 00:00:00' AND '2024-04-11 23:59:59.999999'
- AND "mv_stat_lvl_1_ehr_reasons"."legal_entity_id" = 8185
- GROUP BY "mv_stat_lvl_1_ehr_reasons"."patient_id"
- HAVING (COUNT(DISTINCT mv_stat_lvl_1_ehr_reasons.code) > 0)),
- "reports_cnt" AS (SELECT mv_stat_lvl_0_ehr_diagnostic_reports.patient_id,
- COUNT(DISTINCT mv_stat_lvl_0_ehr_diagnostic_reports.code) AS counter
- FROM "mv_stat_lvl_0_ehr_diagnostic_reports"
- WHERE "mv_stat_lvl_0_ehr_diagnostic_reports"."code" IN
- ('T34001', 'T34006', 'T34024', 'T34011', 'T34013')
- AND "mv_stat_lvl_0_ehr_diagnostic_reports"."asserted_date" BETWEEN '2024-01-01 00:00:00' AND '2024-04-11 23:59:59.999999'
- AND "mv_stat_lvl_0_ehr_diagnostic_reports"."legal_entity_id" = 8185
- GROUP BY "mv_stat_lvl_0_ehr_diagnostic_reports"."patient_id"
- HAVING (COUNT(DISTINCT mv_stat_lvl_0_ehr_diagnostic_reports.code) > 0)),
- "actions_cnt" AS (SELECT mv_stat_lvl_1_ehr_actions.patient_id,
- COUNT(DISTINCT mv_stat_lvl_1_ehr_actions.code) AS counter
- FROM "mv_stat_lvl_1_ehr_actions"
- WHERE "mv_stat_lvl_1_ehr_actions"."code" IN ('K45', 'D45', 'T45')
- AND "mv_stat_lvl_1_ehr_actions"."asserted_date" BETWEEN '2024-01-01 00:00:00' AND '2024-04-11 23:59:59.999999'
- AND "mv_stat_lvl_1_ehr_actions"."legal_entity_id" = 8185
- GROUP BY "mv_stat_lvl_1_ehr_actions"."patient_id"
- HAVING (COUNT(DISTINCT mv_stat_lvl_1_ehr_actions.code) > 0)),
- "groups_counter" AS (SELECT mv_stat_lvl_1_patients.id AS patient_id,
- CASE
- WHEN (DATE_PART('YEAR',
- AGE('2024-04-11 23:59:59.999999', mv_stat_lvl_1_patients.birth_date)) BETWEEN 40 AND 64)
- THEN
- CASE
- WHEN (diagnosis_cnt.diagnoses_a98_counter > 0 OR reasons_cnt.a98_counter > 0) AND
- reports_cnt.counter > 0 AND
- observations_cnt.observations_c1_64_counter > 0 THEN 'done'
- ELSE 'need_done'
- END
- ELSE 'no_need_done'
- END AS visits_from_40_to_64_state,
- CASE
- WHEN (DATE_PART('YEAR',
- AGE('2024-04-11 23:59:59.999999', mv_stat_lvl_1_patients.birth_date)) BETWEEN 65 AND 999)
- THEN
- CASE
- WHEN (diagnosis_cnt.diagnoses_a98_counter > 0 OR reasons_cnt.a98_counter > 0) AND
- (((diagnosis_cnt.diagnoses_c1_65_counter > 0 OR
- diagnosis_cnt.diagnoses_c2_65_counter > 0) AND
- observations_cnt.observations_c1_65_counter > 0) OR
- (diagnosis_cnt.diagnoses_c3_65_counter > 0 AND
- observations_cnt.observations_c2_65_counter > 0)) THEN 'done'
- ELSE 'need_done'
- END
- ELSE 'no_need_done'
- END AS visits_upper_65_state
- FROM "mv_stat_lvl_1_patients"
- LEFT JOIN observations_cnt
- ON mv_stat_lvl_1_patients.id = observations_cnt.patient_id AND
- (observations_cnt.observations_c1_64_counter >= 5 OR
- observations_cnt.observations_c1_65_counter >= 5 OR
- observations_cnt.observations_c2_65_counter >= 6)
- LEFT JOIN diagnosis_cnt ON mv_stat_lvl_1_patients.id = diagnosis_cnt.patient_id AND
- (diagnosis_cnt.diagnoses_c1_64_counter > 0 OR
- diagnosis_cnt.diagnoses_c1_65_counter > 0 OR
- diagnosis_cnt.diagnoses_c2_65_counter > 0 OR
- diagnosis_cnt.diagnoses_c3_65_counter > 0)
- LEFT JOIN reasons_cnt ON reasons_cnt.patient_id = mv_stat_lvl_1_patients.id
- LEFT JOIN reports_cnt ON reports_cnt.patient_id = mv_stat_lvl_1_patients.id
- LEFT JOIN actions_cnt ON actions_cnt.patient_id = mv_stat_lvl_1_patients.id
- WHERE "mv_stat_lvl_1_patients"."legal_entity_id" = 8185
- GROUP BY mv_stat_lvl_1_patients.id, mv_stat_lvl_1_patients.age,
- mv_stat_lvl_1_patients.birth_date, visits_from_40_to_64_state, visits_upper_65_state)
- SELECT mv_stat_lvl_1_declarations.legal_entity_id,
- COUNT(main_counters.diabetes_state) filter (WHERE diabetes_state = 'done') as diabetes_counter,
- COUNT(main_counters.hypertension_state)
- filter (WHERE hypertension_state = 'done') as hypertension_counter,
- COUNT(main_counters.prostate_cancer_state)
- filter (WHERE prostate_cancer_state = 'done') as prostate_cancer_counter,
- COUNT(main_counters.breast_cancer_state)
- filter (WHERE breast_cancer_state = 'done') as breast_cancer_counter,
- COUNT(main_counters.hiv_state) filter (WHERE hiv_state = 'done') as hiv_counter,
- COUNT(main_counters.colorectal_cancer_state)
- filter (WHERE colorectal_cancer_state = 'done') as colorectal_cancer_counter,
- COUNT(groups_counter.visits_from_40_to_64_state)
- filter (WHERE visits_from_40_to_64_state = 'done') as visits_from_40_to_64_counter,
- COUNT(groups_counter.visits_upper_65_state)
- filter (WHERE visits_upper_65_state = 'done') as visits_upper_65_counter,
- COUNT(main_counters.diabetes_state)
- filter (WHERE diabetes_state IN ('done', 'need_done')) as target_diabetes_counter,
- COUNT(main_counters.hypertension_state)
- filter (WHERE hypertension_state IN ('done', 'need_done')) as target_hypertension_counter,
- COUNT(main_counters.prostate_cancer_state)
- filter (WHERE prostate_cancer_state IN ('done', 'need_done')) as target_prostate_cancer_counter,
- COUNT(main_counters.breast_cancer_state)
- filter (WHERE breast_cancer_state IN ('done', 'need_done')) as target_breast_cancer_counter,
- COUNT(main_counters.hiv_state)
- filter (WHERE hiv_state IN ('done', 'need_done')) as target_hiv_counter,
- COUNT(main_counters.colorectal_cancer_state)
- filter (WHERE colorectal_cancer_state IN ('done', 'need_done')) as target_colorectal_cancer_counter,
- COUNT(groups_counter.visits_from_40_to_64_state)
- filter (WHERE visits_from_40_to_64_state IN ('done', 'need_done')) as target_visits_from_40_to_64_counter,
- COUNT(groups_counter.visits_upper_65_state)
- filter (WHERE visits_upper_65_state IN ('done', 'need_done')) as target_visits_upper_65_counter
- FROM "mv_stat_lvl_1_declarations"
- LEFT JOIN main_counters ON mv_stat_lvl_1_declarations.patient_id = main_counters.patient_id
- LEFT JOIN groups_counter ON mv_stat_lvl_1_declarations.patient_id = groups_counter.patient_id
- WHERE "mv_stat_lvl_1_declarations"."legal_entity_id" = 8185
- GROUP BY "mv_stat_lvl_1_declarations"."legal_entity_id"
- LIMIT 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement