SQL Server 2005
I have the following result set:
ID name prop value
--------------------------
1 one Prop1 va1_1_1
1 one Prop2 val_1_2
2 two Prop1 val_2_1
2 two Prop2 val_2_2
3 three Prop2 val_3_2
4 four Prop1 val_4_1
4 four Prop2 val_4_2
How can I flatten it to get an output of
ID name Prop1 Prop2
---------------------------------
1 one val_1_1 val_1_2
2 two val_2_1 val_2_2
3 three val_3_2 NULL
4 four val_4_1 val_4_2
Note: The number of properties (Prop1, Prop2) is arbitrary and can be many.
See MSDN: Using PIVOT and UNPIVOT.
It has a very good example of what you are trying to do.
This will give you the desired result.
Please note, you need to support arbitrary number of prop values. In that case one solution could be to build this script dynamically and execute it.
EDIT:
To make it complete, here is the SQL that would work for arbitrary number of prop values –