Duplication involving SUM, LEFT JOIN and GROUP BY

前端 未结 3 1978
谎友^
谎友^ 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 19:55

    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]
    
    0 讨论(0)
  • 2021-01-20 20:10
    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]
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题