rpt-queries.sql 5.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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