rpt-queries.sql 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. -- no longer active AND device-last-used within :xyz
  2. -- how long since last visit
  3. -- patients created on/after 2022-01-09 without MCP
  4. select count(id)
  5. from client where shadow_pro_id is null and mcp_pro_id is null and created_at::date >= '2022-01-09'
  6. select client.id, client.uid, name_first, name_last, cell_number, source, initiative, client_engagement_status_category,
  7. cpc.commercial_payer_name,
  8. (cpc.auto_medicare_detail_json::json)->'plan_details'->'MD'->'payer_name'
  9. from client left join client_primary_coverage cpc on client.latest_client_primary_coverage_id = cpc.id
  10. where shadow_pro_id is null and mcp_pro_id is null and client.created_at::date >= '2022-01-09'
  11. -- and cpc.auto_medicare_is_match_found = true
  12. order by name_first;
  13. select client.created_at, client.id, client.uid, name_first, name_last, cell_number, source, initiative, client_engagement_status_category,
  14. (select count(id) from note where note.is_cancelled is false and note.is_signed_by_hcp is true and note.client_id = client.id) as num_notes
  15. from client
  16. where
  17. (select count(id) from note where note.is_cancelled is false and note.is_signed_by_hcp is true and note.client_id = client.id) > 0 AND
  18. shadow_pro_id is null and client.created_at::date >= '2022-01-09' and (client.client_engagement_status_category <> 'DUMMY' OR client.client_engagement_status_category is null)
  19. and client.is_part_b_primary = 'YES'
  20. order by created_at desc;
  21. select count(client.id),
  22. (select pro.name_display from pro where pro.id = client.mcp_pro_id) as mcp,
  23. client.mcp_pro_id, client.created_at::date
  24. from client
  25. where
  26. (select count(id) from note where note.is_cancelled is false and note.is_signed_by_hcp is true and note.client_id = client.id) > 0 AND
  27. shadow_pro_id is null and client.created_at::date >= '2022-01-16' and (client.client_engagement_status_category <> 'DUMMY' OR client.client_engagement_status_category is null)
  28. -- and client.is_part_b_primary = 'YES'
  29. group by mcp_pro_id, client.created_at::date
  30. order by client.created_at::date desc, count desc
  31. select count(client.id),
  32. (select pro.name_display from pro where pro.id = client.created_by_pro_id) as creator,
  33. client.created_by_pro_id, client.created_at::date
  34. from client
  35. where
  36. (select count(id) from note where note.is_cancelled is false and note.is_signed_by_hcp is true and note.client_id = client.id) > 0 AND
  37. shadow_pro_id is null and client.created_at::date >= '2022-01-16' and (client.client_engagement_status_category <> 'DUMMY' OR client.client_engagement_status_category is null)
  38. -- and client.is_part_b_primary = 'YES'
  39. group by created_by_pro_id, client.created_at::date
  40. order by client.created_at::date desc, count desc
  41. select client.created_at, client.id, client.uid, name_first, name_last, cell_number, source, initiative, client_engagement_status_category,
  42. (select count(id) from note where note.is_cancelled is false and note.is_signed_by_hcp is true and note.client_id = client.id) as num_notes,
  43. client.mailing_address_state
  44. from client
  45. where
  46. (select count(id) from note where note.is_cancelled is false and note.is_signed_by_hcp is true and note.client_id = client.id) > 0 AND
  47. shadow_pro_id is null and client.created_at::date >= '2022-01-09' and (client.client_engagement_status_category <> 'DUMMY' OR client.client_engagement_status_category is null)
  48. and client.is_part_b_primary = 'YES' and mcp_pro_id = 1175
  49. order by created_at desc;
  50. SELECT
  51. client.name_first, client.name_last, (select pro.name_display from pro where pro.id = client.mcp_pro_id) as mcp, client.most_recent_completed_mcp_note_date, client.client_engagement_status_category,
  52. (SELECT COUNT(*) FROM measurement m WHERE m.client_id = client.id AND m.is_cellular_zero is false and m.ts_date_time::date >= '2021-11-01' AND ts_date_time::date <= '2021-11-30' and m.is_removed is false) as measurements
  53. FROM client
  54. WHERE
  55. id IN (SELECT client_id FROM client_bdt_device) -- have devices
  56. AND is_part_b_primary = 'YES' -- are part b primary
  57. -- have 16+ measurements in dec
  58. AND (SELECT COUNT(*) FROM measurement m WHERE m.client_id = client.id AND m.is_cellular_zero is false and m.ts_date_time::date >= '2021-11-01' AND ts_date_time::date <= '2021-11-30' and m.is_removed is false) > 0
  59. -- have not been see in over 60 days
  60. AND client.most_recent_completed_mcp_note_date < (now() - interval '200 day')::date
  61. SELECT
  62. count(client.id), (select pro.name_display from pro where pro.id = client.mcp_pro_id) as mcp
  63. FROM client
  64. WHERE
  65. id IN (SELECT client_id FROM client_bdt_device) -- have devices
  66. AND is_part_b_primary = 'YES' -- are part b primary
  67. -- have 16+ measurements in dec
  68. AND (SELECT COUNT(distinct(m.created_at::date)) FROM measurement m WHERE m.client_id = client.id AND m.is_cellular_zero is false and m.created_at::date >= '2021-11-01' AND m.created_at::date <= '2021-11-30' and m.is_removed is false) > 0
  69. -- have not been see in over 60 days
  70. AND client.most_recent_completed_mcp_note_date < (now() - interval '100 day')::date
  71. group by mcp_pro_id
  72. -- DATE | MCP | COUNT
  73. UPDATE note SET visit_number = ((SELECT COUNT(*)
  74. FROM note n WHERE n.id < note.id AND n.client_id = note.client_id AND n.is_cancelled IS NOT TRUE AND n.is_core_note IS FALSE) + 1) WHERE is_cancelled IS NOT TRUE;
  75. COPY (
  76. SELECT
  77. name_first,
  78. name_last,
  79. next_mcp_appointment_date,
  80. (SELECT data FROM point WHERE category = 'CHIEF_COMPLAINT' AND point.client_id = client.id ORDER BY point.created_at DESC LIMIT 1) AS latest_chief_complaint,
  81. client_engagement_status_category as status
  82. FROM client
  83. WHERE shadow_pro_id IS NULL
  84. ) TO '/tmp/client_appts.csv' CSV HEADER;
  85. COPY (
  86. SELECT
  87. cl.name_first,
  88. cl.name_last,
  89. appt.raw_date,
  90. appt.status,
  91. mcp.name_first,
  92. mcp.name_last
  93. FROM appointment appt
  94. LEFT JOIN pro mcp ON appt.pro_id = mcp.id
  95. LEFT JOIN client cl ON appt.client_id = cl.id
  96. WHERE appt.raw_date >= '01-05-2022'::DATE
  97. AND cl.shadow_pro_id IS NOT NULL
  98. ) TO '/tmp/appts.csv' CSV HEADER;
  99. UPDATE care_month SET days_between_most_recent_mcp_note_date_and_end_of_care_month =
  100. (((date_trunc('month', start_date) + interval '1 month' - interval '1 day')::date) - most_recent_mcp_note_date )
  101. WHERE days_between_most_recent_mcp_note_date_and_end_of_care_month IS NULL AND most_recent_mcp_note_date IS NOT NULL;
  102. UPDATE care_month SET most_recent_mcp_note_date = (SELECT effective_dateest FROM note WHERE note.client_id = care_month.client_id AND is_signed_by_hcp IS TRUE AND is_cancelled IS NOT TRUE AND effective_dateest < ((date_trunc('month', start_date) + interval '1 month' - interval '1 day')::date) ORDER BY effective_dateest DESC LIMIT 1)
  103. WHERE most_recent_mcp_note_date IS NULL;
  104. UPDATE bill SET effective_date = (SELECT (date_trunc('month', care_month.start_date) + interval '1 month' - interval '1 day')::DATE FROM care_month WHERE bill.care_month_id = care_month.id)
  105. WHERE bill.care_month_id IS NOT NULL and bill.effective_date IS NULL;
  106. UPDATE bill SET effective_date = (SELECT effective_dateest FROM note WHERE bill.note_id = note.id) WHERE bill.note_id IS NOT NULL AND bill.effective_date IS NULL;
  107. SELECT most_recent_mcp_note_date FROM care_month WHERE uid = '6d437394-902e-4899-a2bd-bd1702825461';