Advertisement
temaon_lieto

ALL VIEWS

Feb 2nd, 2024
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- SQL disease_progresses
  2.  
  3. -- mv_encounter_disease_progresses
  4. SELECT receptions.id,
  5.        receptions.patient_id,
  6.        receptions.onset_date,
  7.        CASE
  8.            WHEN (EXISTS (SELECT 1
  9.                          FROM ehr_reasons
  10.                                   JOIN classification_icpc2_items ON ehr_reasons.encounter_id = receptions.id AND
  11.                                                                      ehr_reasons.icpc2_reason_id =
  12.                                                                      classification_icpc2_items.id AND
  13.                                                                      classification_icpc2_items.code::text =
  14.                                                                      'A98'::text)) AND (EXISTS (SELECT 1
  15.                                                                                                 FROM ehr_observations
  16.                                                                                                          JOIN classification_fhir_observation_codes
  17.                                                                                                               ON classification_fhir_observation_codes.id =
  18.                                                                                                                  ehr_observations.codeable_id AND
  19.                                                                                                                  (classification_fhir_observation_codes.code::text = ANY
  20.                                                                                                                   (ARRAY ['39156-5'::character varying::text, '14743-9'::character varying::text, '8462-4'::character varying::text, '8480-6'::character varying::text]))))
  21.                THEN true
  22.            ELSE false
  23.            END AS prof_check
  24. FROM receptions
  25.          JOIN user_patients ON user_patients.id = receptions.patient_id
  26.          JOIN personalities
  27.               ON personalities.id = user_patients.personality_id AND personalities.type::text = 'Patient::Person'::text
  28.          JOIN declarations ON declarations.personality_id = personalities.id AND
  29.                               declarations.declaration_status_id = ((SELECT declaration_statuses.id
  30.                                                                      FROM declaration_statuses
  31.                                                                      WHERE declaration_statuses.code::text = 'ACTIVE'::text))
  32.  
  33.  
  34.  
  35. -- mv_connected_assocs_disease_progresses
  36. SELECT combined_data.id,
  37.        combined_data.patient_id,
  38.        combined_data.service_code,
  39.        combined_data.performed_date,
  40.        combined_data.assoc_type
  41. FROM (SELECT ehr_direction_service_requests.id,
  42.              ehr_direction_service_requests.patient_id,
  43.              ehr_ehealth_services.code                          AS service_code,
  44.              ehr_direction_service_requests.ehealth_inserted_at AS performed_date,
  45.              0                                                  AS assoc_type
  46.       FROM ehr_direction_service_requests
  47.                JOIN ehr_ehealth_services
  48.                     ON ehr_ehealth_services.id = ehr_direction_service_requests.service_requestable_id AND
  49.                        ehr_direction_service_requests.service_requestable_type::text = 'Ehr::Ehealth::Service'::text AND
  50.                        (ehr_direction_service_requests.status::text = ANY
  51.                         (ARRAY ['active'::character varying::text, 'completed'::character varying::text]))
  52.       UNION
  53.       SELECT ehr_diagnostic_reports.id,
  54.              ehr_diagnostic_reports.patient_id,
  55.              ehr_ehealth_services.code     AS service_code,
  56.              ehr_diagnostic_reports.issued AS perfomed_date,
  57.              1                             AS assoc_type
  58.       FROM ehr_diagnostic_reports
  59.                JOIN ehr_ehealth_services ON ehr_diagnostic_reports.service_id = ehr_ehealth_services.id AND
  60.                                             ehr_diagnostic_reports.ehealth_status = 1
  61.       UNION
  62.       SELECT ehr_observations.id,
  63.              ehr_observations.patient_id,
  64.              NULL::character varying AS service_code,
  65.              ehr_observations.issued AS performed_date,
  66.              2                       AS assoc_type
  67.       FROM ehr_observations
  68.                JOIN classification_fhir_observation_codes
  69.                     ON classification_fhir_observation_codes.id = ehr_observations.codeable_id AND
  70.                        classification_fhir_observation_codes.code::text = '14743-9'::text) combined_data
  71.          JOIN user_patients ON user_patients.id = combined_data.patient_id
  72.          JOIN personalities
  73.               ON personalities.id = user_patients.personality_id AND personalities.type::text = 'Patient::Person'::text
  74.          JOIN declarations ON declarations.personality_id = personalities.id AND
  75.                               declarations.declaration_status_id = ((SELECT declaration_statuses.id
  76.                                                                      FROM declaration_statuses
  77.                                                                      WHERE declaration_statuses.code::text = 'ACTIVE'::text))
  78.  
  79.  
  80. -- SQL chronic diseases
  81. -- mv_stat_conditions
  82.  
  83. SELECT ehr_conditions.encounter_id,
  84.        ehr_conditions.patient_id,
  85.        ehr_conditions.employee_id,
  86.        classification_icpc2_items.code,
  87.        classification_icpc2_items.title_translations -> 'uk'::text AS label,
  88.        receptions.asserted_date
  89. FROM ehr_conditions
  90.          JOIN receptions ON receptions.id = ehr_conditions.encounter_id AND receptions.ehealth_status = 1
  91.          JOIN classification_icpc2_items
  92.               ON classification_icpc2_items.type::text = 'Classification::Icpc2::Condition'::text AND
  93.                  classification_icpc2_items.id = ehr_conditions.icpc2_code_id
  94. WHERE ehr_conditions.employee_id IS NOT NULL
  95.   AND ((classification_icpc2_items.code::text = ANY
  96.         (ARRAY ['T90'::character varying, 'K85'::character varying, 'K86'::character varying, 'K87'::character varying, 'R96'::character varying]::text[])) OR
  97.        classification_icpc2_items.code::text ~~ 'P%'::text)
  98.  
  99. -- mv_stat_imt_diseases
  100.         SELECT DISTINCT ON (ehr_observations.id, ehr_observations.patient_id, classification_fhir_observation_codes.code)
  101.           'ІМТ' as label,
  102.           ehr_observations.encounter_id,
  103.           ehr_observations.employee_id,
  104.           ehr_observations.patient_id,
  105.           classification_fhir_observation_codes.code,
  106.           receptions.asserted_date,
  107.           CASE WHEN classification_fhir_observation_codes.code = '39156-5' THEN
  108.             NULLIF(REGEXP_REPLACE(ehr_observations.surveillance_value ->>'value','[^0-9.]+','','g')::text, '')::float
  109.           ELSE
  110.             COALESCE(NULLIF(REGEXP_REPLACE(ehr_observations.surveillance_value ->>'value','[^0-9.]+','','g')::text, '')::float, 1::float) / POWER((COALESCE(NULLIF(REGEXP_REPLACE(imt_table.observation_value, '[^0-9.]+', '', 'g')::text,'')::numeric, 1::numeric))::float * 0.01::float, 2)
  111.           END AS imt_index
  112.         FROM "ehr_observations"
  113.           INNER JOIN classification_fhir_observation_codes ON classification_fhir_observation_codes."id" = ehr_observations."codeable_id" AND classification_fhir_observation_codes.code IN ('39156-5', '29463-7')
  114.           INNER JOIN "classification_fhir_items" ON "classification_fhir_items"."id" = "classification_fhir_observation_codes"."quantity_unit_id" AND "classification_fhir_items"."type" IN ('Classification::Fhir::ValueSet::UcumUnit')
  115.           INNER JOIN receptions ON receptions.id = ehr_observations.encounter_id AND receptions.ehealth_status = 1
  116.           LEFT OUTER JOIN (SELECT ehr_observations.id,
  117.                             ehr_observations.patient_id,
  118.                             classification_fhir_observation_codes.title_translations -> 'uk' as observation_title,
  119.                             ehr_observations.surveillance_value ->> 'value'                  AS "observation_value",
  120.                             classification_fhir_items.code                                   as unit_code
  121.                           FROM ehr_observations
  122.                             INNER JOIN classification_fhir_observation_codes ON classification_fhir_observation_codes."id" = ehr_observations."codeable_id" AND classification_fhir_observation_codes.code = '8302-2'
  123.                             INNER JOIN "classification_fhir_items" ON "classification_fhir_items"."id" = "classification_fhir_observation_codes"."quantity_unit_id" AND "classification_fhir_items"."type" IN ('Classification::Fhir::ValueSet::UcumUnit') ORDER BY ehr_observations.id DESC) AS imt_table
  124.                           ON imt_table.patient_id = ehr_observations.patient_id AND classification_fhir_observation_codes.code = '29463-7'
  125.           WHERE ehr_observations.id IS NOT NULL AND ehr_observations.employee_id IS NOT NULL AND nullif(trim(ehr_observations.surveillance_value ->> 'value'), '') IS NOT NULL
  126.           AND (classification_fhir_observation_codes.code = '39156-5' AND (COALESCE(NULLIF(REGEXP_REPLACE(ehr_observations.surveillance_value ->>'value', '[^0-9.]+', '', 'g')::text, '')::NUMERIC, 1::NUMERIC)::float > 25::float) OR (classification_fhir_observation_codes.code IN ('29463-7') AND imt_table.patient_id IS NOT NULL AND COALESCE(NULLIF(REGEXP_REPLACE(imt_table.observation_value, '[^0-9.]+', '', 'g')::text, '')::NUMERIC, 0::NUMERIC) > 0::NUMERIC AND COALESCE(NULLIF(REGEXP_REPLACE(ehr_observations.surveillance_value ->> 'value', '[^0-9.]+', '', 'g')::text, '')::float, 1::float) / POWER((COALESCE(NULLIF(REGEXP_REPLACE(imt_table.observation_value, '[^0-9.]+', '', 'g')::text, '')::numeric, 1::numeric))::float * 0.01::float, 2) > 25::float))
  127.  
  128.  
  129.  
  130. -- mv_stat_receptions_conditions
  131. SELECT receptions.id,
  132.        receptions.legal_entity_id,
  133.        receptions.patient_id,
  134.        medication_prescriptions.ehealth_status AS medication_prescriptions_ehealth_status,
  135.        medication_prescriptions.program_id     AS prescription_program_id,
  136.        classification_icpc2_items.id           AS icpc2_code_id
  137. FROM receptions
  138.          JOIN ehr_conditions ON ehr_conditions.encounter_id = receptions.id
  139.          LEFT JOIN medication_prescriptions ON medication_prescriptions.encounter_id = receptions.id AND
  140.                                                (medication_prescriptions.ehealth_status = ANY (ARRAY [0, 1]))
  141.          JOIN classification_icpc2_items ON classification_icpc2_items.id = ehr_conditions.icpc2_code_id AND
  142.                                             classification_icpc2_items.type::text =
  143.                                             'Classification::Icpc2::Condition'::text AND
  144.                                             classification_icpc2_items.code::text >= 'K29'::text AND
  145.                                             classification_icpc2_items.code::text <= 'K99'::text
  146. WHERE receptions.ehealth_status = 1
  147.  
  148. -- mv_stat_declarations
  149. SELECT DISTINCT ON (declarations.id) declarations.id,
  150.                                      declarations.employee_id,
  151.                                      declarations.personality_id,
  152.                                      declarations.division_id,
  153.                                      declarations.start_date,
  154.                                      declarations.end_date,
  155.                                      "Parties"."LegalEntityId" AS legal_entity_id,
  156.                                      user_patients.id          AS patient_id,
  157.                                      personalities.birth_date,
  158.                                      declaration_statuses.code AS declaration_status_code,
  159.                                      receptions.asserted_date
  160. FROM declarations
  161.          JOIN dbo."Employees" ON "Employees"."Id" = declarations.employee_id
  162.          JOIN declaration_statuses ON declaration_statuses.id = declarations.declaration_status_id
  163.          JOIN dbo."Parties" ON "Parties"."Id" = "Employees"."PartyId"
  164.          JOIN personalities
  165.               ON personalities.id = declarations.personality_id AND personalities.type::text = 'Patient::Person'::text
  166.          JOIN user_patients ON user_patients.personality_id = personalities.id
  167.          LEFT JOIN receptions ON receptions.patient_id = user_patients.id AND receptions.ehealth_status = 1
  168. WHERE declaration_statuses.code::text = ANY
  169.       (ARRAY ['ACTIVE'::character varying::text, 'COMPLETED'::character varying::text])
  170. ORDER BY declarations.id, receptions.asserted_date DESC
  171.  
  172.  
  173. -- mv_stat_patient_personalities
  174. SELECT user_patients.id,
  175.        personalities.id                                                                                   AS personalities_id,
  176.        declarations.id                                                                                    AS declaration_id,
  177.        declarations.employee_id,
  178.        personalities.gender,
  179.        user_patients.is_smoking,
  180.        personalities.birth_date,
  181.        EXTRACT(year FROM age(personalities.birth_date::timestamp with time zone))                         AS age,
  182.        concat_ws(' '::text, personalities.last_name,
  183.                  personalities.first_name)                                                                AS original_name,
  184.        concat_ws(' '::text, personalities.last_name, personalities.first_name, personalities.second_name) AS full_name
  185. FROM user_patients
  186.          JOIN personalities ON user_patients.personality_id = personalities.id
  187.          JOIN declarations ON declarations.personality_id = personalities.id
  188.          JOIN declaration_statuses ON declaration_statuses.id = declarations.declaration_status_id
  189. WHERE declaration_statuses.code::text = 'ACTIVE'::text
  190.   AND personalities.type::text = 'Patient::Person'::text
  191.  
  192.  
  193. -- TOTALS --
  194.  
  195. -- mv_stat_legal_entity_total_completed_service_requests
  196. SELECT ehr_direction_service_requests.legal_entity_id,
  197.        count(ehr_direction_service_requests.id) AS counter
  198. FROM ehr_direction_service_requests
  199. WHERE ehr_direction_service_requests.status::text = 'completed'::text
  200.   AND ehr_direction_service_requests.category_code::text = 'counselling'::text
  201. GROUP BY ehr_direction_service_requests.legal_entity_id
  202.  
  203.  
  204. -- mv_stat_legal_entity_total_reception_conditions
  205. SELECT receptions.legal_entity_id,
  206.        count(DISTINCT receptions.patient_id) AS counter
  207. FROM receptions
  208.          JOIN ehr_conditions ON ehr_conditions.encounter_id = receptions.id
  209.          JOIN classification_icpc2_items ON classification_icpc2_items.id = ehr_conditions.icpc2_code_id AND
  210.                                             classification_icpc2_items.type::text =
  211.                                             'Classification::Icpc2::Condition'::text AND
  212.                                             classification_icpc2_items.code::text >= 'K29'::text AND
  213.                                             classification_icpc2_items.code::text <= 'K99'::text
  214. WHERE receptions.ehealth_status = 1
  215. GROUP BY receptions.legal_entity_id
  216.  
  217. -- mv_stat_legal_entity_total_receptions
  218. SELECT receptions.legal_entity_id,
  219.        count(receptions.id) AS counter
  220. FROM receptions
  221. WHERE receptions.ehealth_status = 1
  222. GROUP BY receptions.legal_entity_id
  223.  
  224. -- mv_stat_legal_entity_total_service_requests
  225. SELECT ehr_direction_service_requests.legal_entity_id,
  226.        count(ehr_direction_service_requests.id) AS counter
  227. FROM ehr_direction_service_requests
  228. WHERE (ehr_direction_service_requests.status::text = ANY
  229.        (ARRAY ['completed'::character varying::text, 'active'::character varying::text]))
  230.   AND ehr_direction_service_requests.category_code::text = 'counselling'::text
  231. GROUP BY ehr_direction_service_requests.legal_entity_id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement