2017-04-01 9 views
0

を変換する必要があります:私は必要なものSQL、私は次のデータ・フォーマットを持つテーブルがあるのMicrosoft SQL Server 2014 で働いているテーブル

|customerId | action | amount | isSuccessful | paymentCount | 
|____1____ |__ W__ |____10__|____0____  |_____2________| 
|____1____ |__ D__ |____20__|____1____  |_____3________| 
|____1____ |__ W__ |____30__|____1____  |_____1________| 
|____1____ |__ D__ |____40__|____0____  |_____1________| 

は、この形式でレポートを行うことです。

|customerId|depositAmount|withdrawalAmount|depositAttemptCount|withdrawalAttemptCount| 
|____1____ |______20 ____|________30_____ |________4_______ _|________3__________ | 

selectを使用してテーブルを変換する方法はありますか?

私は助けていただきありがとうございます。

+0

はこちらをご覧: http://stackoverflow.com/questions/15931607/convert-rows-to-columns-using-pivot-in-sql -server – IngoB

答えて

3

あなたはここで、条件付き集約を使用することができます。

select customerId, 
    sum(case when action = 'D' and isSuccessful = 1 then amount else 0 end) as depositAmount, 
    sum(case when action = 'W' and isSuccessful = 1 then amount else 0 end) as withdrawalAmount, 
    sum(case when action = 'D' then paymentCount else 0 end) as depositAttemptCount, 
    sum(case when action = 'W' then paymentCount else 0 end) as withdrawalAttemptCount 
from your_table 
group by customerId; 
+0

それは正しい解決策でした!どうもありがとうございました! – Nicolas

関連する問題