Hey everyone,
I have the following query that i used a bunch of unions to get different counts. I want to compare all the self pay accounts to all the accounts per site location.
This is very slow due, and since i am pretty much running the same query for both types, is there away to get rid of the unions or make this more efficient?
I have the following query that i used a bunch of unions to get different counts. I want to compare all the self pay accounts to all the accounts per site location.
This is very slow due, and since i am pretty much running the same query for both types, is there away to get rid of the unions or make this more efficient?
Code:
Select
sSite,
Category,
Sum(Num_of_Accounts) As Num_of_Accounts,
Sum(Collection_Accounts) As Collection_Accounts,
Sum(Num_of_Statements) Num_of_Statements,
SUM(Total_Charges_Amount) Total_Charges_Amount
from (
--------------------------------
--Self Pay---------------------
SELECT
ACCOUNT.CPCODE sSite,
'Self Pay Patients' Category,
Count(ACCOUNT.CODE) Num_of_Accounts,
SUM(CASE WHEN (ACCOUNT.EXCEPTION IN ('CA','CP')) THEN 1 ELSE 0 END) Collection_Accounts,
sum(ACCOUNT.STATEMENTS) Num_of_Statements,
0 Total_Charges_Amount
FROM
ACCOUNT ACCOUNT
WHERE
(ACCOUNT.BILLSTATUS='S')
AND to_char(to_date(ACCOUNT.LOGDATE,'j'),'YYYYMMDD') between '20070101' and '20070131'
GROUP BY
ACCOUNT.CPCODE
UNION
SELECT
ACCOUNT.CPCODE sSite,
'Self Pay Patients' Category,
0 Num_of_Accounts,
0 Collection_Accounts,
0 Num_of_Statements,
SUM(CHARGES.PRAMOUNT) Total_Charges_Amount
FROM
ACCOUNT ACCOUNT
JOIN ECI_LTD.CHARGES CHARGES ON ((ACCOUNT.CPCODE=CHARGES.CPCODE) AND (ACCOUNT.CODE=CHARGES.ACCOUNT) AND (CHARGES.TYPE='C') AND (CHARGES.SPLITFLAG IS NULL))
WHERE
(ACCOUNT.BILLSTATUS='S')
AND to_char(to_date(ACCOUNT.LOGDATE,'j'),'YYYYMMDD') between '20070101' and '20070131'
GROUP BY
ACCOUNT.CPCODE
--------------------------------
--ALL Accounts------------------
UNION
SELECT
ACCOUNT.CPCODE sSite,
'All Patients' Category,
Count(ACCOUNT.CODE) Num_of_Accounts,
SUM(CASE WHEN (ACCOUNT.EXCEPTION IN ('CA','CP')) THEN 1 ELSE 0 END) Collection_Accounts,
sum(ACCOUNT.STATEMENTS) Num_of_Statements,
0 Total_Charges_Amount
FROM
ACCOUNT ACCOUNT
WHERE to_char(to_date(ACCOUNT.LOGDATE,'j'),'YYYYMMDD') between '20070101' and '20070131'
GROUP BY
ACCOUNT.CPCODE
UNION
SELECT
ACCOUNT.CPCODE sSite,
'All Patients' Category,
0 Num_of_Accounts,
0 Collection_Accounts,
0 Num_of_Statements,
SUM(CHARGES.PRAMOUNT) Total_Charges_Amount
FROM
ACCOUNT ACCOUNT
JOIN ECI_LTD.CHARGES CHARGES ON ((ACCOUNT.CPCODE=CHARGES.CPCODE) AND (ACCOUNT.CODE=CHARGES.ACCOUNT) AND (CHARGES.TYPE='C') AND (CHARGES.SPLITFLAG IS NULL))
WHERE to_char(to_date(ACCOUNT.LOGDATE,'j'),'YYYYMMDD') between '20070101' and '20070131'
GROUP BY
ACCOUNT.CPCODE
--------------------------------
) U
Group By sSite, Category