Advertisement
temaon_lieto

test sql

Sep 30th, 2024
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. WITH cte_employees AS (SELECT dbo."Employees"."Id" as id,
  2. dbo."Employees"."PartyId" as party_id,
  3. dbo."Parties"."LegalEntityId" as legal_entity_id,
  4. CONCAT_WS(' ', dbo."Parties"."LastName", dbo."Parties"."FirstName",
  5. dbo."Parties"."SecondName") AS full_name
  6. FROM "dbo"."Employees"
  7. INNER JOIN "dbo"."Parties" ON "dbo"."Employees"."PartyId" = "dbo"."Parties"."Id"
  8. WHERE dbo."Parties"."LegalEntityId" = 8657
  9. AND "dbo"."Employees"."EmployeeStatusRefValue" = 'APPROVED'),
  10. cte_declarations AS (SELECT declarations.id,
  11. declarations.employee_id,
  12. declarations.personality_id,
  13. user_patients.id as patient_id,
  14. cte_employees.legal_entity_id,
  15. declaration_statuses.code AS status_code,
  16. declarations.start_date,
  17. personalities.email,
  18. CONCAT_WS(' ', personalities.last_name, personalities.first_name,
  19. personalities.second_name) AS patient_full_name
  20. FROM declarations
  21. INNER JOIN user_patients
  22. ON user_patients.personality_id = declarations.personality_id
  23. INNER JOIN personalities ON personalities.id = declarations.personality_id
  24. INNER JOIN cte_employees ON cte_employees.id = declarations.employee_id
  25. INNER JOIN declaration_statuses
  26. ON declaration_statuses.id = declarations.declaration_status_id
  27. WHERE cte_employees.legal_entity_id = 8657
  28. AND declaration_statuses.code = 'ACTIVE'
  29. AND declarations.employee_id IS NOT NULL
  30. AND declarations.division_id IS NOT NULL),
  31. cte_reception_counters AS (SELECT receptions.legal_entity_id,
  32. receptions.patient_id,
  33. COUNT(receptions.patient_id) as counter
  34. FROM receptions
  35. WHERE receptions.legal_entity_id = 8657
  36. AND receptions.asserted_date >= '2024-08-30'::DATE
  37. -- AND update_date < '2024-09-30'::DATE
  38. AND patient_id IS NOT NULL
  39. AND employee_id IS NOT NULL
  40. AND legal_entity_id IS NOT NULL
  41. GROUP BY receptions.legal_entity_id, receptions.patient_id)
  42.  
  43. SELECT
  44. -- COUNT(cte_declarations.id)
  45. cte_declarations.id,
  46. cte_declarations.legal_entity_id,
  47. cte_employees.full_name,
  48. cte_declarations.employee_id,
  49. cte_declarations.personality_id,
  50. cte_declarations.patient_full_name,
  51. cte_reception_counters.counter,
  52. contacts.phone_number,
  53. cte_declarations.email
  54. FROM cte_declarations
  55. JOIN mv_stat_lvl_0_patient_contacts contacts on cte_declarations.personality_id = contacts.id
  56. INNER JOIN cte_employees on cte_declarations.employee_id = cte_employees.id
  57. LEFT OUTER JOIN cte_reception_counters ON cte_declarations.patient_id = cte_reception_counters.patient_id
  58.  
  59. WHERE cte_reception_counters.counter IS NULL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement