Question: I want the payer code, payer name, payer category code, example inpatient visit, inpatient count, facility name, and facility code. Right now my code only

I want the payer code, payer name, payer category code, example inpatient visit, inpatient count, facility name, and facility code. Right now my code only outputs the following: payer code, payer name, payer category code, example inpatient visit. Please provide a SQL query that it will include inpatient count, facility name, and facility code in the output as well.
SELECT DISTINCT
code,
name,
payer_category_code,
'https://'||(select value
from configuration_option
where config_key = 'customerId'
)
||'.test'||visits.visit_id||'123' example_inpatient_visit
FROM payer p
LEFT JOIN (SELECT
split_part(payers,',',1) payer
,max(visit_id) visit_id
FROM visit
WHERE (pre_disch_admit_status =1 OR admit_status =1)
AND admit_date > NOW()- INTERVAL '30 Days'
AND facility is not null
AND loc_poc is not null
GROUP BY 1
)visits ON visits.payer = p.code
UNION
SELECT DISTINCT
vp.plan_id
, vp.company_name
, 'NOT MAPPED'
,'https://'||(select value
from configuration_option
where config_key = 'customerId'
)
||'.test'||visits.visit_id||'123' example_inpatient_visit
FROM visit_payer vp
LEFT JOIN payer p ON p.code = vp.plan_id
LEFT JOIN (SELECT
split_part(payers,',',1) payer
,max(visit_id) visit_id
FROM visit
WHERE (pre_disch_admit_status =1 OR admit_status =1)
AND admit_date > NOW()- INTERVAL '30 Days'
AND facility is not null
AND loc_poc is not null
GROUP BY 1
)visits ON visits.payer = vp.plan_id
WHERE p.code IS NULL
AND vp.creation_date >= NOW()- INTERVAL '30 days'
ORDER BY 1,2,3;
NOTE: You can get the inpatientcount
FROM general_adt
WHERE admit_date > NOW()- INTERVAL '30 Days'
AND patient_class IN ('I','INPATIENT','Inpatient','IN')
GROUP BY 1,2;
Here is some information about my tables; The payer table has the following: code, name, payer_category_code. The visit table has the following: visit_id, account_no, admit_status, loc_poc. The visit_payer table has the following: plan_id, visit_id. The configuration_option table has the following: config_key. The facility table has the following: code, name. The location table has the following: facility_code, code, name. The general_adt table has the following: account_no, patient_id, visit_no. Please let me know if you have any questions!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!