This is part of a 5 part union where I am trying to count the number of unique people and the amount of money spent
in 3 program areas. If a person is in program a, I want them counted in program A but not counted in B or C. If they are in B and not A then I want them counted in b and if they are in C then I only want them counted if they weren't in A or B. However, I need the total amount of monies spent by program. When I hard code the dates it works ok but when I added the report table so that we can run a report on demand to pick up the data it ground to a standstill. Can anyone see what I am doing incorrectly? Thanks
in 3 program areas. If a person is in program a, I want them counted in program A but not counted in B or C. If they are in B and not A then I want them counted in b and if they are in C then I only want them counted if they weren't in A or B. However, I need the total amount of monies spent by program. When I hard code the dates it works ok but when I added the report table so that we can run a report on demand to pick up the data it ground to a standstill. Can anyone see what I am doing incorrectly? Thanks
Code:
select
SHORT_DESC,
Age_category,
0 COUNT_CLIENTCASE,
sum(EXPENDED_AMT) SUM_EXPENDED_AMT,
HRE_REPORTS_ID
from (
SELECT DISTINCT
NVL(CL.CLIENT_ID,999999) || NVL(C.FILE_NO,999999) clientcase,
(case when to_number(TRUNC(REP.EFFECTIVE_DATE)
- cl.BIRTH_DATE) / 365 < 30 then
'Youth'
when to_number(TRUNC(REP.EFFECTIVE_DATE)
- cl.BIRTH_DATE) / 365 >= 30 then
'Adult'
else
'Unknown'
end ) Age_category,
PR.PAY_REQUIRE_ID,
C.FILE_NO,
PB.SHORT_DESC,
PR.EXPENDED_AMT,
HRE_REPORTS_ID
FROM
PAY_USER.PAY_REQUIRE PR,
PAY_USER.PAYMENT P,
PAY_USER.CLIENT CL,
PAY_USER.CASE C,
PAY_USER.PROGRAM_BENEFIT PB,
PAY_USER.HRE_REPORTS REP
WHERE
P.PAYMENT_ID = PR.PAYMENT_ID
AND CL.CLIENT_ID(+) = PR.CLIENT_ID
AND C.CASE_ID = P.CASE_ID
AND P.CREATED_BY_ID<>1
AND TRUNC(NVL(P.PER_START_DATE,P.PAYMENT_DATE)) >= TRUNC(REP.EFFECTIVE_DATE)
AND TRUNC(NVL(P.PER_START_DATE,P.PAYMENT_DATE)) < TRUNC(REP.EFFECTIVE_TO)
AND PB.PROGRAM_BENEFIT_ID = PR.REQ_TYPE_ID
and REP.REPORT_ID = 9517
and PR.REQ_TYPE_ID = 842
and P.CASE_ID in
(SELECT P2.CASE_ID
FROM
PAY_USER.PAY_REQUIRE PR2,
PAY_USER.PAYMENT P2
WHERE
P2.PAYMENT_ID = PR2.PAYMENT_ID
AND P.CASE_ID = P2.CASE_ID
AND P2.CREATED_BY_ID <>1
AND TRUNC(NVL(P2.PER_START_DATE,P2.PAYMENT_DATE)) >= TRUNC(REP.EFFECTIVE_DATE)
AND TRUNC(NVL(P2.PER_START_DATE,P2.PAYMENT_DATE)) < TRUNC(REP.EFFECTIVE_TO)
and PR2.REQ_TYPE_ID in (841,843) ))
group by SHORT_DESC, Age_category, HRE_REPORTS_ID