4

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.

Omar
  • 39,496
  • 45
  • 145
  • 213

3 Answers3

3

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.

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 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 -

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)
YetAnotherUser
  • 9,156
  • 3
  • 39
  • 53
  • I've seen that before. But it seems the example (and all other PIVOT examples) revolve around using some type of aggregate function (`SUM`, `MAX`). I don't see how I can apply that to this. – Omar Jun 17 '11 at 18:23
  • I've updated the answer to show how you can use MAX() to get what you want. – YetAnotherUser Jun 17 '11 at 18:57
  • That is not what OP is looking for. OP wants Pivot with Dynamic Columns. – Adriano Carneiro Jun 17 '11 at 19:01
  • @Ardian - That is why I've stated, OP will need to generate this script dynamically to support arbitrary number of prop values. – YetAnotherUser Jun 17 '11 at 19:21
  • This works. Final question, how can I have the query concatenate the content in `value`if there exists more than one row with the same `ID` and `prop`? – Omar Jun 17 '11 at 22:11
  • I don't think you can concatenate strings, it only works on [Aggregate Functions](http://msdn.microsoft.com/en-us/library/ms173454.aspx). You can get the same effect by concatenating the desired items in a temp table and pivoting this temp table instead. – YetAnotherUser Jun 17 '11 at 22:31
0
    SELECT ID,Name, (SELECT prop FROM table t1 WHERE prop=prop1 AND ID = table.id) AS Prop1,
    (SELECT prop FROM table t2 WHERE prop=prop2 AND ID = table.id) AS Prop2
    FROM table
Frank Pearson
  • 872
  • 5
  • 16
0

This is what you are looking for:

Community
  • 1
  • 1
Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123