-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnswer 2.sql
More file actions
32 lines (26 loc) · 1.46 KB
/
Answer 2.sql
File metadata and controls
32 lines (26 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use DBA_TEST
select DEPT_NAME, TBL_TRANSACTIONS.PATIENT_ID, PATIENT_NAME,
-- replace insurance name with SELF-PAY when its null
ISNULL(TBL_INSURANCES.INSURANCE_NAME, 'SELF-PAY') AS INSURANCE_NAME,
-- replace NULL insurance payment to 0
SUM(CHARGES - (ISNULL(TBL_TRANSACTIONS.INSURANCE_PAYMENT, 0) + ISNULL(PATIENT_PAYMENT, 0)) ) AS OUTSTANDING_BALANCE
from
-- inner join transactions and patients
((((TBL_TRANSACTIONS
inner join TBL_PATIENTS on TBL_TRANSACTIONS.PATIENT_ID = TBL_PATIENTS.PATIENT_ID)
-- add inner join physicians
inner join TBL_PHYSICIANS on TBL_TRANSACTIONS.PHYSICIAN_ID = TBL_PHYSICIANS.PHYSICIAN_ID)
-- add inner join departments
inner join TBL_DEPARTMENTS on TBL_PHYSICIANS.DEPT_ID = TBL_DEPARTMENTS.DEPT_ID)
-- left outer join insurances to also get the patients with no insurance id's into the result
left outer join TBL_INSURANCES on TBL_PATIENTS.INSURANCE_ID = TBL_INSURANCES.INSURANCE_ID)
-- perform analysis on the transactions that are only outstanding
where TBL_TRANSACTIONS.TX_ID IN (SELECT TX_ID FROM TBL_TRANSACTIONS
where
(TBL_TRANSACTIONS.CHARGES > ISNULL(TBL_TRANSACTIONS.INSURANCE_PAYMENT, 0) +
TBL_TRANSACTIONS.PATIENT_PAYMENT)
)
-- grouping due to details organized by department are required
GROUP BY DEPT_NAME, TBL_TRANSACTIONS.PATIENT_ID, PATIENT_NAME, TBL_INSURANCES.INSURANCE_NAME
-- showing results in reverse order of outstanding balance in each department
ORDER BY DEPT_NAME ,OUTSTANDING_BALANCE DESC