Advertisement
temaon_lieto

Page 2 sql

Mar 29th, 2024 (edited)
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. WITH "visits" AS (SELECT mv_stat_lvl_0_receptions.id, mv_stat_lvl_0_receptions.employee_id, mv_stat_lvl_0_receptions.asserted_date, mv_stat_lvl_0_receptions.patient_id FROM "mv_stat_lvl_0_receptions" WHERE "mv_stat_lvl_0_receptions"."legal_entity_id" = 867 AND "mv_stat_lvl_0_receptions"."asserted_date" BETWEEN '2024-01-01 00:00:00' AND '2024-03-29 23:59:59.999999'), "declarations_cnt" AS (SELECT mv_stat_lvl_1_declarations.employee_id, COUNT(DISTINCT visits.patient_id) as counter, COUNT(DISTINCT mv_stat_lvl_1_declarations.id) as total_counter FROM "mv_stat_lvl_1_declarations" JOIN visits ON mv_stat_lvl_1_declarations.employee_id = visits.employee_id WHERE "mv_stat_lvl_1_declarations"."legal_entity_id" = 867 GROUP BY "mv_stat_lvl_1_declarations"."employee_id") SELECT mv_stat_lvl_0_employees.*, COALESCE(declarations_cnt.counter, 0) as visits_counter, COALESCE(declarations_cnt.total_counter, 0)as total_counter FROM "mv_stat_lvl_0_employees" LEFT JOIN declarations_cnt ON mv_stat_lvl_0_employees.id = declarations_cnt.employee_id WHERE "mv_stat_lvl_0_employees"."legal_entity_id" = 867 AND "mv_stat_lvl_0_employees"."employee_type_ref_value" = 'DOCTOR' AND "mv_stat_lvl_0_employees"."employee_status_ref_value" = 'APPROVED' ORDER BY full_name ASC LIMIT 15 OFFSET 0
  2.  
  3.  
  4.  
  5. WITH "receptions" AS (SELECT "mv_stat_lvl_0_receptions".* FROM "mv_stat_lvl_0_receptions" WHERE "mv_stat_lvl_0_receptions"."legal_entity_id" = 867 AND "mv_stat_lvl_0_receptions"."asserted_date" BETWEEN '2024-01-01 00:00:00' AND '2024-03-29 23:59:59.999999') SELECT mv_stat_lvl_1_patients.id,
  6. mv_stat_lvl_1_patients.employee_id,
  7. mv_stat_lvl_1_patients.age,
  8. mv_stat_lvl_1_patients.full_name,
  9. mv_stat_lvl_1_patients.gender,
  10. mv_stat_lvl_1_patients.personalities_id,
  11. COUNT(receptions.id) as visits_counter,
  12. MAX(receptions.asserted_date) as last_visit_date,
  13. CASE
  14.     WHEN COUNT(receptions.id) > 0 THEN
  15.         CASE WHEN COUNT(receptions.id) filter ( where receptions.employee_id = mv_stat_lvl_1_patients.employee_id ) > 0
  16.             THEN
  17.               'own_doctor_visit'
  18.             ELSE
  19.               'general_visit'
  20.         END
  21. ELSE
  22.     'without_visit'
  23. END AS  visits_state FROM "mv_stat_lvl_1_patients" LEFT JOIN receptions ON mv_stat_lvl_1_patients.id = receptions.patient_id
  24. LEFT JOIN mv_stat_lvl_0_patient_contacts ON mv_stat_lvl_1_patients.personalities_id = mv_stat_lvl_0_patient_contacts.id WHERE "mv_stat_lvl_1_patients"."legal_entity_id" = 867 AND (mv_stat_lvl_0_patient_contacts.full_street ILIKE '%но%') GROUP BY mv_stat_lvl_1_patients.id,
  25. mv_stat_lvl_1_patients.employee_id,
  26. mv_stat_lvl_1_patients.age,
  27. mv_stat_lvl_1_patients.full_name,
  28. mv_stat_lvl_1_patients.gender,
  29. mv_stat_lvl_1_patients.personalities_id ORDER BY mv_stat_lvl_1_patients.full_name ASC LIMIT 15 OFFSET 0
  30.  
  31. SELECT "mv_stat_lvl_0_patient_contacts".* FROM "mv_stat_lvl_0_patient_contacts" WHERE "mv_stat_lvl_0_patient_contacts"."id" = 7698
  32.  
  33. SELECT "mv_stat_lvl_0_employees".* FROM "mv_stat_lvl_0_employees" WHERE "mv_stat_lvl_0_employees"."id" = 2639
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.       CREATE MATERIALIZED VIEW IF NOT EXISTS mv_stat_lvl_0_patient_contacts WITH (autovacuum_enabled = off) AS
  41.        WITH phones AS (
  42.           SELECT
  43.               phones.dialable_id,
  44.               COALESCE((MAX(phones.number) filter (where(phones.phone_type_id = 3))), (MAX(phones.number) filter (where(phones.phone_type_id = 11)))) AS number
  45.           FROM "phones"
  46.           WHERE "phones"."dialable_type" = 'Patient::Person' AND "phones"."phone_type_id" IN (3, 11)
  47.           GROUP BY phones.dialable_id
  48.       ), addresses AS (
  49.         SELECT
  50.           street_types.title_translations -> 'uk' as street_type,
  51.           addresses.addressable_id as personality_id,
  52.           addresses.street,
  53.           addresses.building,
  54.           addresses.apartment,
  55.           addresses.zip,
  56.           addresses.country_id,
  57.           countries.title_translations -> 'uk' as country_name,
  58.           addresses.district_id,
  59.           districts.title_translations -> 'uk' as district_name,
  60.           addresses.settlement_id,
  61.           settlements.title_translations -> 'uk' as settlement_name,
  62.           addresses.region_id,
  63.           regions.title_translations -> 'uk' as region_name
  64.         FROM
  65.           "addresses"
  66.             LEFT JOIN street_types ON addresses.street_type_id = street_types.id
  67.             LEFT JOIN countries ON countries.id = addresses.country_id
  68.             LEFT JOIN districts ON districts.id = addresses.district_id
  69.             LEFT JOIN settlements ON settlements.id = addresses.settlement_id
  70.             LEFT JOIN regions ON regions.id = addresses.region_id
  71.         WHERE "addresses"."addressable_type" = 'Patient::Person' AND "addresses"."address_type_id" IN ((SELECT address_types.id FROM address_types WHERE address_types.code = 'RESIDENCE'))
  72.       )
  73.  
  74.       SELECT DISTINCT ON (personalities.id)
  75.           personalities.id,
  76.           phones.number as phone_number,
  77.           addresses.street_type,
  78.           addresses.street,
  79.           addresses.building,
  80.           addresses.apartment,
  81.           addresses.country_id,
  82.           addresses.district_id,
  83.           addresses.settlement_id,
  84.           addresses.region_id,
  85.           addresses.country_name,
  86.           addresses.district_name,
  87.           addresses.settlement_name,
  88.           addresses.region_name,
  89.           CONCAT_WS(', ', NULLIF(addresses.street_type, ''), NULLIF(addresses.street, ''), NULLIF(addresses.building, ''), NULLIF(addresses.apartment, '')) as full_street
  90.       FROM "personalities"
  91.           LEFT JOIN "addresses" ON "addresses"."personality_id" = "personalities"."id"
  92.           LEFT JOIN "phones" ON "phones"."dialable_id" = "personalities"."id"
  93.       WHERE (phones.number IS NOT NULL OR addresses.personality_id IS NOT NULL) AND "personalities"."type" IN ('Patient::Person')
  94.             WITH NO DATA;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement