SQL Server:
I have a query that returns the table of the most popular goods delivered to customers:
CustomerId TotalPrice, Name, Rank
===================================
1 100 TV 1
2 50 Coffee 1
1 70 PC 2
3 30 Clothes 1
1 60 Games 3
1 50 Drinks 4
This data means that CustomerId = 1 most popular goods are: TV, PC, Games, Drinks
How may I transform this data to TOP-3 good of every customer like that:
CustomerId TOP-1 TOP-1-Price TOP-2 TOP-2-Price TOP-3 TOP-3-Price
====================================================================
1 TV 100 PC 70 Games 60
2 Coffee 50 NULL NULL NULL NULL
3 Clothes 30 NULL NULL NULL NULL
I guess I may use multiple select/join using rank = 1, rank = 2, rank = 3. Is there more efficient approach?