Hey guys,
I have two queries that use derive tables, and I need to combine totals together to get a grand total. I can do this manually on the spreadsheet, but it is so time consuming.
First I have this query for payouts:
I have a similiar second query to find total payments:
I need to combine these queries somehow, and end up with one total field that is TOTAL_PAYOUT + TOTAL_AMOUNT for each county.
Can anyone help?
I have two queries that use derive tables, and I need to combine totals together to get a grand total. I can do this manually on the spreadsheet, but it is so time consuming.
First I have this query for payouts:
Code:
SELECT A.CTY_ID_NM AS COUNTY_NAME
,COUNT(*) AS COUNT
,SUM(A.TOTAL_AMOUNT) as TOTAL_PAYOUT
FROM (SELECT E.CTY_ID_NM AS CTY_ID_NM
,A.RECIP_TYPE_CD AS RECIP_TYPE_CD
,A.RECIP_SSN_NBR AS RECIP_SSN_NBR
,SUM(B.GROSS_AMT) AS TOTAL_AMOUNT
FROM DSNP.PR01_T_RECIP_SYS A
,DSNP.PR01_T_PAYOUT B
,DSNP.PR01_T_MBR_CITY C
,DSNP.PR01_T_ZIP D
,DSNP.PR01_T_CTY E
WHERE A.RECIP_SSN_NBR = B.RECIP_SSN_NBR
AND A.BENEF_SEQ_NBR = B.BENEF_SEQ_NBR
AND A.RECIP_SSN_NBR = C.MBR_SSN_NBR
AND C.STATE_ID_CD = D.STATE_ID_CD
AND C.POST_ZIP_CD = D.POST_ZIP_CD
AND D.CTY_ID_CD = E.CTY_ID_CD
AND C.MBR_ADDR_SEQ_NBR = 1
AND B.PYMT_TYPE in ('D', 'P')
AND B.PYMT_DATE BETWEEN '2009-07-01'
AND '2010-06-30'
GROUP BY
E.CTY_ID_NM
,A.RECIP_TYPE_CD
,A.RECIP_SSN_NBR ) as A
GROUP BY A.CTY_ID_NM
I have a similiar second query to find total payments:
Code:
SELECT A.CTY_ID_NM AS COUNTY_NAME
,COUNT(*) AS COUNT
,SUM(A.ANTY_PYMT_TOT_AMT) AS TOTAL_AMOUNT
FROM (SELECT E.CTY_ID_NM AS CTY_ID_NM
,A.RECIP_TYPE_CD AS RECIP_TYPE_CD
,A.RECIP_SSN_NBR AS RECIP_SSN_NBR
,SUM(B.ANTY_PYMT_TOT_AMT) AS ANTY_PYMT_TOT_AMT
FROM DSNP.PR01_T_RECIP_SYS A
,DSNP.PR01_T_ANTY_PYMT B
,DSNP.PR01_T_MBR_CITY C
,DSNP.PR01_T_ZIP D
,DSNP.PR01_T_CTY E
WHERE A.RECIP_SSN_NBR = B.RECIP_SSN_NBR
AND A.BENEF_SEQ_NBR = B.BENEF_SEQ_NBR
AND A.RECIP_SSN_NBR = C.MBR_SSN_NBR
AND C.STATE_ID_CD = D.STATE_ID_CD
AND C.POST_ZIP_CD = D.POST_ZIP_CD
AND D.CTY_ID_CD = E.CTY_ID_CD
AND B.ANTY_PYMT_STAT_CD = 'A'
AND C.MBR_ADDR_SEQ_NBR = 1
AND B.ANTY_PYMT_DT BETWEEN '2009-07-01'
AND '2010-06-30'
GROUP BY
E.CTY_ID_NM
,A.RECIP_TYPE_CD
,A.RECIP_SSN_NBR ) as A
GROUP BY A.CTY_ID_NM
I need to combine these queries somehow, and end up with one total field that is TOTAL_PAYOUT + TOTAL_AMOUNT for each county.
Can anyone help?