Advertisement
temaon_lieto

SQL disease_progresses/employees

Feb 2nd, 2024
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- http://localhost:4000/api/v1/disease_progresses/employees
  2.  
  3. WITH "limited_assocs" AS (SELECT
  4.     service_code,
  5.     patient_id,
  6.     assoc_type
  7. FROM
  8.     mv_connected_assocs_disease_progresses
  9. WHERE performed_date BETWEEN '2024-01-01 00:00:00' AND '2024-02-02 23:59:59.999999'), "limited_encounters" AS (SELECT
  10.     patient_id,
  11.     onset_date,
  12.     prof_check
  13. FROM mv_encounter_disease_progresses
  14. WHERE onset_date BETWEEN '2024-01-01 00:00:00' AND '2024-02-02 23:59:59.999999') SELECT DISTINCT mv_stat_patient_personalities.id,
  15. mv_stat_patient_personalities.full_name,
  16. mv_stat_patient_personalities.employee_id,
  17. mv_stat_patient_personalities.birth_date,
  18. DATE_PART('YEAR', AGE('2024-02-02 23:59:59.999999', mv_stat_patient_personalities.birth_date)) as age,
  19. "mv_stat_employees_parties"."DivisionId" as division_id,
  20.   CASE
  21.     WHEN (DATE_PART('YEAR', AGE('2024-02-02 23:59:59.999999', mv_stat_patient_personalities.birth_date)) >= 40 AND mv_stat_patient_personalities.gender = 0 OR
  22.           DATE_PART('YEAR', AGE('2024-02-02 23:59:59.999999', mv_stat_patient_personalities.birth_date)) >= 50 AND mv_stat_patient_personalities.gender = 1)
  23.     THEN CASE
  24.            WHEN EXISTS (
  25.                   SELECT 1 FROM limited_assocs WHERE
  26.                     limited_assocs.patient_id = mv_stat_patient_personalities.id AND
  27.                     limited_assocs.service_code in ('T34006','T34011','T34013','T34024','T340001','T34004') AND
  28.                     limited_assocs.assoc_type in (0,1)
  29.                 )
  30.                   THEN 2
  31.                 ELSE 1
  32.     END ELSE 0
  33.   END hypertension,
  34.   CASE
  35.     WHEN DATE_PART('YEAR', AGE('2024-02-02 23:59:59.999999', mv_stat_patient_personalities.birth_date)) >= 45
  36.       THEN CASE
  37.         WHEN EXISTS (
  38.           SELECT 1 FROM limited_assocs WHERE
  39.             limited_assocs.patient_id = mv_stat_patient_personalities.id AND
  40.             limited_assocs.service_code in ('T34005','T34023','T34025','T34038') AND
  41.             limited_assocs.assoc_type in (0,1)
  42.         ) OR EXISTS (
  43.           SELECT 1 FROM limited_assocs WHERE
  44.             limited_assocs.patient_id = mv_stat_patient_personalities.id AND
  45.             limited_assocs.assoc_type in (2)
  46.       )
  47.         THEN 2
  48.       ELSE 1
  49.     END ELSE 0
  50.   END diabetes,
  51.   CASE
  52.     WHEN DATE_PART('YEAR', AGE('2024-02-02 23:59:59.999999', mv_stat_patient_personalities.birth_date)) >= 40 AND mv_stat_patient_personalities.gender = 0
  53.       THEN CASE
  54.         WHEN EXISTS (
  55.           SELECT 1 FROM limited_assocs WHERE
  56.             limited_assocs.patient_id = mv_stat_patient_personalities.id AND
  57.             limited_assocs.service_code in ('U67002','Y34011','Y34003') AND
  58.             limited_assocs.assoc_type in (0)
  59.       )
  60.         THEN 2
  61.       ELSE 1
  62.     END ELSE 0
  63.   END prostate_cancer,
  64.   CASE
  65.     WHEN DATE_PART('YEAR', AGE('2024-02-02 23:59:59.999999', mv_stat_patient_personalities.birth_date)) BETWEEN 50 AND 69 AND mv_stat_patient_personalities.gender = 1
  66.       THEN CASE
  67.         WHEN EXISTS (
  68.           SELECT 1 FROM limited_assocs WHERE
  69.             limited_assocs.patient_id = mv_stat_patient_personalities.id AND
  70.             limited_assocs.service_code in ('TX41973','59300-00','55070-00','55076-00','Х41941') AND
  71.             limited_assocs.assoc_type in (0)
  72.       )
  73.           THEN 2
  74.         ELSE 1
  75.     END ELSE 0
  76.   END breast_cancer,
  77.   CASE
  78.     WHEN DATE_PART('YEAR', AGE('2024-02-02 23:59:59.999999', mv_stat_patient_personalities.birth_date)) BETWEEN 50 AND 75 AND mv_stat_patient_personalities.gender = 0
  79.       THEN CASE
  80.         WHEN EXISTS (
  81.           SELECT 1 FROM limited_assocs WHERE
  82.             limited_assocs.patient_id = mv_stat_patient_personalities.id AND
  83.             limited_assocs.service_code in ('D36003','D67006','32084-00','32090-00','32084-02','32090-02') AND
  84.             limited_assocs.assoc_type in (0)
  85.       )
  86.         THEN 2
  87.       ELSE 1
  88.     END ELSE 0
  89.   END colorectal_cancer,
  90.   CASE
  91.     WHEN DATE_PART('YEAR', AGE('2024-02-02 23:59:59.999999', mv_stat_patient_personalities.birth_date)) >= 14
  92.       THEN CASE
  93.         WHEN EXISTS (
  94.           SELECT 1 FROM limited_assocs WHERE
  95.             limited_assocs.patient_id = mv_stat_patient_personalities.id AND
  96.             limited_assocs.service_code in ('B33006','B33012') AND
  97.             limited_assocs.assoc_type in (0,1)
  98.       )
  99.         THEN 2
  100.       ELSE 1
  101.     END ELSE 0
  102.   END hiv,
  103.   (EXISTS (SELECT 1 FROM limited_encounters WHERE
  104.     limited_assocs.patient_id = limited_encounters.patient_id AND
  105.     DATE_PART('YEAR', AGE('2024-02-02 23:59:59.999999', mv_stat_patient_personalities.birth_date)) BETWEEN 40 and 64)) as from_40_to_64_visits,
  106.   (EXISTS (SELECT 1 FROM limited_encounters WHERE
  107.     limited_assocs.patient_id = limited_encounters.patient_id AND
  108.     limited_encounters.prof_check = true AND
  109.     DATE_PART('YEAR', AGE('2024-02-02 23:59:59.999999', mv_stat_patient_personalities.birth_date)) BETWEEN 40 and 64)) as from_40_to_64_prof_visits,
  110.   (EXISTS (SELECT 1 FROM limited_encounters WHERE
  111.     limited_assocs.patient_id = limited_encounters.patient_id AND
  112.     DATE_PART('YEAR', AGE('2024-02-02 23:59:59.999999', mv_stat_patient_personalities.birth_date)) >= 65)) as upto_65_visits,
  113.   (EXISTS (SELECT 1 FROM limited_encounters WHERE
  114.     limited_assocs.patient_id = limited_encounters.patient_id AND
  115.     limited_encounters.prof_check = true AND
  116.     DATE_PART('YEAR', AGE('2024-02-02 23:59:59.999999', mv_stat_patient_personalities.birth_date)) >= 65)) AS upto_65_prof_visits FROM "mv_stat_patient_personalities" INNER JOIN "mv_stat_declarations" ON "mv_stat_declarations"."patient_id" = "mv_stat_patient_personalities"."id" AND ("mv_stat_declarations"."declaration_status_code" = 'ACTIVE') INNER JOIN "mv_stat_employees_parties" ON "mv_stat_employees_parties"."Id" = "mv_stat_patient_personalities"."employee_id" LEFT OUTER JOIN limited_assocs ON limited_assocs.patient_id = mv_stat_patient_personalities.id WHERE "mv_stat_patient_personalities"."id" IS NOT NULL AND ("mv_stat_employees_parties"."LegalEntityId" = 3228)
  117.  
  118. ELECT "mv_stat_employees_parties".* FROM "mv_stat_employees_parties" WHERE ("mv_stat_employees_parties"."LegalEntityId" = 3228) AND ("mv_stat_employees_parties"."EmployeeStatusRefValue" = 'APPROVED') AND ("mv_stat_employees_parties"."EmployeeTypeRefValue" = 'DOCTOR')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement