MySQL - sum column value(s) based on row from the same table

前端 未结 3 1216
我寻月下人不归
我寻月下人不归 2020-12-07 13:46

I\'m trying to get \'Cash\', \'Check\' and \'Credit Card\' totals in new columns based on ProductID from the same table.

Table - Payments

+---------         


        
3条回答
  •  死守一世寂寞
    2020-12-07 14:37

    I think you're making this a bit more complicated than it needs to be.

    SELECT
        ProductID,
        SUM(IF(PaymentMethod = 'Cash', Amount, 0)) AS 'Cash',
        -- snip
        SUM(Amount) AS Total
    FROM
        Payments
    WHERE
        SaleDate = '2012-02-10'
    GROUP BY
        ProductID
    

提交回复
热议问题