2017-04-01 1 views
0

ich mit Microsoft SQL Server arbeiten bin 2014 Ich habe eine Tabelle mit Format nächsten Daten:SQL, müssen eine Tabelle verwandeln

|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________| 

Was ich brauche, ist mit diesem Format berichten:

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

Wie es möglich ist, Tabelle mit Select zu "transformieren"?

Ich würde jede Hilfe zu schätzen wissen.

+0

Werfen Sie einen Blick hier: http://stackoverflow.com/questions/15931607/convert-rows-to-columns-using-pivot-in-sql -server – IngoB

Antwort

3

Sie hier bedingte Aggregation verwenden können:

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

Es war die richtige Lösung! Vielen Dank! – Nicolas

Verwandte Themen