I have a problem involving a SUM, LEFT OUTER JOIN and GROUP BY commands, but can\'t figure out where my error is.
I have two tables, one for customer transactions an
The query will be counting the transaction amount twice for 2007 due to the fact you have two claims, so the transaction amount will be counted twice.
i.e. the returned data being used is:
Customer | Transaction Year | Transaction Amount | Claim Amount
----------------------------------------------------------------
A | 2007 | 100 | 30
A | 2007 | 100 | 40
A | 2008 | 80 |
A | 2008 | 50 |
A | 2009 | 210 | 110
Something like the following, although not pretty, should resolve the problem:
SELECT
t.Customer
,t.Year
,[Transaction Amount] = SUM(t.[Transaction Amount])
,[Claim Amount] = c.[Claim Amount]
FROM
Transactions t
LEFT JOIN (
SELECT
Customer
,Year
,SUM([Claim Amount])
FROM
Claims
GROUP BY
Customer, Year
) c ON c.Customer = t.Customer c.Year = t.Year
GROUP BY t.Customer, t.Year, c.[Claim Amount]
With T as (
SELECT Customer,
[Transaction Year],
sum(Amount) AS TransactionAmount
FROM Transactions
GROUP BY Customer, [Transaction Year]
), C AS
SELECT Customer,
[Claim Year],
sum(Amount) as ClaimAmount
FROM Claims
GROUP BY Customer, [Claim Year]
)
SELECT T.Customer,
[Transactions Year],
TransactionAmount,
ClaimAmount
FROM T
LEFT JOIN C ON C.Customer = T.Customer
AND [Transactions Year] = [Claim Year]
So the first step to see what is happening is to remove the SUMs and just select the transaction amount and claim amount. That way you can see what data is being returned. You'll see that the join on A/2007 will have the transaction amount twice, since it's joining each row to the claims table.
One solution is to use subquerys, like you said, to do the SUMs separately prior to joining.
SELECT
Transactions.Customer,
Transactions.Year,
SumTransaction,
SumClaim
FROM (
select Customer, Year, sum(Transaction Amount) SumTransaction
from Transactions
group by Customer, Year
) Transactions
LEFT JOIN (
select Customer, Year, sum(Claim Amount) sumClaim
from Claims
group by Customer, Year
) Claims
ON Claims.Customer = Transactions.Customer
AND Transactions.Year = Claims.Year
Another possible solution given your restrictions:
SELECT
Transactions.Customer,
Transactions.Year,
SUM(Transaction Amount),
(SELECT SUM(Claim Amount) from Claims where Claims.Customer = Transactions.Customer and Claims.Year = Transactions.Year)
FROM
Transactions
GROUP BY
Customer, Year
Third possible solution!! This one does not require any subqueries! See this SQL Fiddle
select
t.Customer,
t.Year,
sum(distinct t.Amount),
sum(c.Amount)
from
Transactions t
left join Claims c
on t.Customer = c.Customer
and t.Year = c.year
group by
t.Customer,
t.Year