2017-11-03 17 views
0

私は、それぞれの新しい列を作成するために各合計でグループを取得しようとしています。結果としてグループ合計金額のmysql内訳

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 

が、私はこれは、下記のスクリーンショットのような結果の一種である workbench screenshot

を探していますされていないものを、それは私が excel screenshot

+1

あなたはあなたの質問を編集し、すべてのデータを質問に直接含めることができますか? –

+0

私はそれを行います。 –

答えて

1

あなたが使用することができますを探していますものですこれを達成するためにSUMIFを一緒に使用します。

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 
関連する問題