我正在尝试在每个总和上创建一个组来创建每个新列。
SELECT
distinct settlement_id,
SUM(amount) AS `Order`,
SUM(amount) AS `Refund`,
SUM(amount) AS `Servicefee`,
SUM(amount) AS `other_Transaction`
FROM
settlements
WHERE
transaction_type = 'order'
OR transaction_type = 'refund'
OR transaction_type = 'ServiceFee'
OR transaction_type = 'other-transaction'
GROUP BY settlement_id ASC , transaction_type ASC
因为结果不是我想要的
这是一种结果,如下面的截图,这就是我要找的
答案 0 :(得分:1)
您可以同时使用SUM
和IF
来实现此目标。
SELECT settlement_id,
SUM(IF(transaction_type = 'order',amount,0)) AS 'Order',
SUM(IF(transaction_type = 'refund',amount,0)) AS 'Refund',
SUM(IF(transaction_type = 'ServiceFee',amount,0)) AS 'Servicefee',
SUM(IF(transaction_type = 'other-transaction',amount,0)) AS 'other_Transaction'
FROM settlements
GROUP BY settlement_id