I am using a Union statement to combine 2 aggregate (sum) select statements but I really want to sum the whole lot where the ParentID is the same number.
My statement as it stands is:
SELECT Ref_Code AS ParentID,
SUM(LMR_CurrentLoanBal) AS CurrentLoanBalance
FROM Loan_MasterReference
WHERE ISNULL(LMR_KeyIDLink,0) = 0
GROUP BY Ref_Code
UNION ALL
SELECT LMR_KeyIDLink AS ParentID,
SUM(LMR_CurrentLoanBal) AS CurrentLoanBalance
FROM Loan_MasterReference
WHERE ISNULL(LMR_KeyIDLink,0) <> 0
GROUP BY LMR_KeyIDLink
ORDER BY Ref_Code
Any help will be much appreciated.
Thanks
Matt
My statement as it stands is:
SELECT Ref_Code AS ParentID,
SUM(LMR_CurrentLoanBal) AS CurrentLoanBalance
FROM Loan_MasterReference
WHERE ISNULL(LMR_KeyIDLink,0) = 0
GROUP BY Ref_Code
UNION ALL
SELECT LMR_KeyIDLink AS ParentID,
SUM(LMR_CurrentLoanBal) AS CurrentLoanBalance
FROM Loan_MasterReference
WHERE ISNULL(LMR_KeyIDLink,0) <> 0
GROUP BY LMR_KeyIDLink
ORDER BY Ref_Code
Any help will be much appreciated.
Thanks
Matt