Duplication involving SUM, LEFT JOIN and GROUP BY

前端 未结 3 2007
谎友^
谎友^ 2021-01-20 19:39

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

3条回答
  •  深忆病人
    2021-01-20 20:11

    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
    

提交回复
热议问题