I’m trying to merge multiple rows into one column.
I have a table:
COST_ID PRICE_ID PRICE_TIER COST
1 4 1 850.00
2 4 2 950.00
3 4 3 1000.00
4 7 1 250.00
5 7 2 275.00
6 7 3 300.00
And I want to the output one row for each price_id, it should look something like this:
ID PriceID C1ID C1Cost C2ID C2Cost C3ID C3Cost
1 4 1 850.00 2 950.00 3 1000.00
2 7 4 250.00 5 275.00 6 300.00
Thank you for your help.
My preferred solution would be using a conditional aggregate like so:
However this can also be done with SQL-Server’s PIVOT function. However it requires a UNION because PIVOT does not allow for multiple aggregates in a single PIVOT.
Demonstrative SQL Fiddle