|
@@ -0,0 +1,105 @@
|
|
|
+CREATE OR REPLACE VIEW dna_patient_list AS
|
|
|
+select
|
|
|
+ c.id,
|
|
|
+ c.uid,
|
|
|
+ c.shadow_pro_id,
|
|
|
+ c.client_engagement_status_category,
|
|
|
+ c.chart_number,
|
|
|
+ c.name_first,
|
|
|
+ c.name_last,
|
|
|
+ (c.name_first ||' '||c.name_last) as display_name,
|
|
|
+ c.dob,
|
|
|
+
|
|
|
+ c.mcp_pro_id,
|
|
|
+ c.default_na_pro_id,
|
|
|
+ (mcp_pro.name_first||' '||mcp_pro.name_last) as mcp_pro_display_name,
|
|
|
+
|
|
|
+ c.mailing_address_state,
|
|
|
+ c.mpb_remaining,
|
|
|
+ c.has_mcp_done_onboarding_visit,
|
|
|
+ c.next_mcp_appointment_id,
|
|
|
+
|
|
|
+ c.latest_client_primary_coverage_id,
|
|
|
+ cpc.manual_determination_category,
|
|
|
+ cpc.auto_medicare_is_match_found,
|
|
|
+ cpc.auto_medicaid_is_match_found,
|
|
|
+ cpc.auto_commercial_is_match_found,
|
|
|
+ cpc.deductible_amount,
|
|
|
+ cpc.carrier_free_text,
|
|
|
+
|
|
|
+ mcdp.name as mcd_payer_name,
|
|
|
+ commp.name as commercial_payer_name,
|
|
|
+
|
|
|
+
|
|
|
+ cpc.is_partbprimary,
|
|
|
+ cpc.plan_type,
|
|
|
+ cpc.is_manually_determined,
|
|
|
+
|
|
|
+ c.age_in_years,
|
|
|
+ c.sex,
|
|
|
+ c.usual_bmi_max,
|
|
|
+ c.most_recent_weight_at,
|
|
|
+ c.most_recent_bp_at,
|
|
|
+ c.created_at,
|
|
|
+ c.most_recent_completed_mcp_note_id,
|
|
|
+ c.most_recent_completed_mcp_note_date,
|
|
|
+ c.email_address,
|
|
|
+ c.default_mcp_company_pro_id,
|
|
|
+ c.default_mcp_company_pro_payer_id,
|
|
|
+ c.default_mcp_company_location_id,
|
|
|
+ c.initiative,
|
|
|
+ c.effective_client_primary_coverage_id,
|
|
|
+
|
|
|
+ c.most_recent_cellular_bp_sbp_mm_hg,
|
|
|
+ c.most_recent_cellular_bp_dbp_mm_hg,
|
|
|
+ c.most_recent_cellular_bp_value_irregular,
|
|
|
+ c.most_recent_cellular_bp_value_pulse,
|
|
|
+ c.most_recent_cellular_weight_value,
|
|
|
+ c.next_mcp_appointment_date,
|
|
|
+
|
|
|
+
|
|
|
+ (select content from client_memo where client_id = c.id order by created_at desc LIMIT 1) as last_memo_content,
|
|
|
+ (select category from client_memo where client_id = c.id order by created_at desc LIMIT 1) as last_memo_category,
|
|
|
+ (select name_first||' '||name_last from pro where id = (select client_memo.created_by_pro_id from client_memo where client_id = c.id order by created_at desc LIMIT 1)) as last_memo_created_by_pro_display_name,
|
|
|
+ (select count(*) from note where client_id = c.id and is_cancelled is not true ) as active_notes_count,
|
|
|
+
|
|
|
+ nmcpa.status as next_mcp_appointment_status,
|
|
|
+ nmcpa.raw_date as next_mcp_appointment_raw_date,
|
|
|
+ nmcpa.raw_start_time as next_mcp_appointment_start_time,
|
|
|
+
|
|
|
+ mrcn.uid as most_recent_completed_mcp_note_uid,
|
|
|
+ mrcn.effective_dateest as most_recent_completed_mcp_note_effective_dateest,
|
|
|
+
|
|
|
+ mrcn.follow_up_not_needed_memo as most_recent_completed_note_followup_memo,
|
|
|
+
|
|
|
+ mrbpm.created_at as most_recent_cellular_bp_measurement_at,
|
|
|
+ mrwm.created_at as most_recent_cellular_weight_measurement_at,
|
|
|
+
|
|
|
+ c.created_at as client_created_at,
|
|
|
+
|
|
|
+ (creator.name_first||' '||creator.name_last) as creator_display_name,
|
|
|
+
|
|
|
+ (select created_at from client_pro_change where new_pro_id = c.mcp_pro_id and responsibility_type = 'MCP' order by created_at DESC LIMIT 1) as mcp_assigned_on,
|
|
|
+
|
|
|
+ (dna.name_first|| ' '||dna.name_last ) as dna_display_name,
|
|
|
+ (SELECT sh.status FROM shipment sh LEFT JOIN supply_order so ON so.shipment_id = sh.id WHERE so.product_id = 4 AND so.client_id = c.id ORDER BY sh.created_at DESC LIMIT 1) as temparature_gun_delivery_status,
|
|
|
+ (SELECT sh.status FROM shipment sh LEFT JOIN supply_order so ON so.shipment_id = sh.id WHERE so.product_id = 5 AND so.client_id = c.id ORDER BY sh.created_at DESC LIMIT 1) as oximeter_delivery_status,
|
|
|
+
|
|
|
+ (select value from measurement where client_id = c.id AND bdt_measurement_id IS NOT NULL AND ts IS NOT NULL AND is_cellular_zero IS FALSE AND is_active IS TRUE AND label = 'Wt. (lbs.)' order by ts desc LIMIT 1) as last_measurement_of_type_weight,
|
|
|
+ (select value from measurement where client_id = c.id AND bdt_measurement_id IS NOT NULL AND ts IS NOT NULL AND is_cellular_zero IS FALSE AND is_active IS TRUE AND label = 'BP' order by ts desc LIMIT 1) as last_measurement_of_type_bp,
|
|
|
+
|
|
|
+ c.is_assigned_cellular_bp_device as has_bp_device,
|
|
|
+ c.is_assigned_cellular_weight_scale_device has_weight_scale_device
|
|
|
+from client c
|
|
|
+ left join pro mcp_pro on mcp_pro.id = c.mcp_pro_id
|
|
|
+ left join client_primary_coverage cpc on cpc.id = c.latest_client_primary_coverage_id
|
|
|
+ left join payer mcdp on mcdp.id = cpc.mcd_payer_id
|
|
|
+ left join payer commp on commp.id = cpc.commercial_payer_id
|
|
|
+ left join note lmcpa on lmcpa.id = c.previous_mcp_appointment_id
|
|
|
+ left join pro lmcpa_pro on lmcpa_pro.id = lmcpa.hcp_pro_id
|
|
|
+ left join appointment nmcpa on nmcpa.id = c.next_mcp_appointment_id
|
|
|
+ left join note mrcn on mrcn.id = c.most_recent_completed_mcp_note_id
|
|
|
+ left join measurement mrwm on mrwm.id = c.most_recent_weight_measurement_id
|
|
|
+ left join measurement mrbpm on mrbpm.id = c.most_recent_bp_measurement_id
|
|
|
+ left join pro creator on creator.id = c.created_by_pro_id
|
|
|
+ left join pro dna on dna.id = c.default_na_pro_id;
|