SELECT Y.LEA,
Y.[Funding Type],
Y.[FY],
Y.[Current Allocation],
Y.[Current Contract],
Y.[Current Expenditure],
Z.[Total Contingency]
FROM
( SELECT c.county_name AS LEA,
s.source_of_fund AS [Funding Type],
substring(f.psc_no, 6, 4)AS [FY],
SUM(f.current_allocation) AS [Current Allocation],
SUM(f.current_contract) AS [Current Contract],
SUM(current_expenditure) AS [Current Expenditure]
FROM source_of_fund_transaction f
JOIN county c
ON substring(f.psc_no, 1, 2)=c.county_number
JOIN source_of_fund s
ON f.sof_id=s.sof_id
WHERE NOT (f."TRANSACTION_TYPE"='C/O Decline'
OR f."TRANSACTION_TYPE"='C/O Posting'
OR f."TRANSACTION_TYPE"='Post Contract')
GROUP BY c.county_name,
s.source_of_fund,
substring(f.psc_no, 6, 4)
) Y
LEFT OUTER JOIN
(SELECT county_name AS LEA,
source_of_fund AS [Funding Type],
substring(psc_no, 6, 4) AS FY,
SUM(amount) AS [Total Contingency]
FROM project_contingency p
JOIN source_of_fund s
ON p.sof_id=s.sof_id
JOIN county c
ON substring(psc_no, 1, 2)=county_number
GROUP BY s.source_of_fund,
county_name,
substring(psc_no, 6, 4)
) Z
ON Y.LEA = Z.LEA
AND Y.[Funding Type] = Z.[Funding Type]
AND Y.[FY] = Z.[FY]
ORDER BY Y.LEA,
Y.[Funding Type],
Y.[FY]
|