Reputation: 3451
If I have the following table:
Cust Prod Qty
===== ==== ===
Bob Apple 3
Bob Orange 2
Bob Banana 4
Rob Apple 2
Rob Orange 1
Bob Apple 2
How can I get the following result with the table data as the column names:
Prod Bob Rob
====== === ===
Apple 5 2
Orange 2 1
Banana 4 null
Upvotes: 2
Views: 570
Reputation: 7189
SELECT * from t
pivot
(
sum(Qty)
for [Cust] in ([Bob],[Rob])
)as p;
GROUP BY PROD
Upvotes: 2