See MSDN: Using PIVOT and UNPIVOT .
He has a very good example of what you are trying to do.
This will give you the desired result.
Select * From ( Select ID,name,prop,value from YourTable ) P PIVOT ( max(value) For Prop in (Prop1,Prop2) ) as pvt Order By ID
Please note: you need to support an arbitrary number of prop values. In this case, one solution could be to create this script dynamically and execute it.
EDIT: To do this, here is the SQL that will work for an arbitrary number of prop values -
Declare @Value as NVarChar(Max) Set @Value = 'Select * From ( Select ID,name,prop,value from YourTable ) P PIVOT ( max(value) For Prop in (' Select @Value = @Value + Prop + ',' from ( Select Distinct Prop From YourTable) YT Set @Value = Left(@Value, Len(@Value)-1) Set @Value = @Value + ') ) as pvt Order By ID' Exec(@Value)
source share