I have a table in SQL like this.
OrderID ItemID ItemPrice ItemType
1 A 100 1
1 B 50 1
1 C 10 0
2 A 100 1
2 F 60 0
3 G 10 0
So I want to get out put like this?
OrderID ItemPrice -Type= 1 ItemPrice -Type= 0
1 150 10
2 10 60
3 10
Do you have any idea about the SQL command to use?
I think it is group by order ID and Item type.
What you are doing is a pivot transformation. There are a few ways to do it, but my favorite way is using CASE inside SUM:
This scales nicely if you have more than two types. All you have to do is add another
SUM(...)line in your select list without having to change the rest of the query.I think this will perform well, since the calculations in the SELECT list can be done without incurring additional row scans or lookups. That’s the downside of self-joins and sub-selects.