Duplication involving SUM, LEFT JOIN and GROUP BY

前端 未结 3 1982
谎友^
谎友^ 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: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]
    

提交回复
热议问题