>>>>Given a SQL2008 table structure:
>>>>
>>>>Id, ColumnId, RowId
>>>>1 1 1
>>>>2 1 2
>>>>3 2 1
>>>>4 2 2
>>>>
>>>>How do I convert it to:
>>>>
>>>>Row, Column1 Column2
>>>> 1 1 3
>>>> 2 2 4
>>>>
>>>>In case it's relevant the values are all GUIDS rather than ints and there will be more columns (and, obviously, rows)
>>>>(Sorry about the spacing....)
>>>
>>>How do you go from the input to the output - Don't see it
>>
>>Id of Row1, Column1 is 1. Id of Row2, Column2 is 4 etc......
>>So the output table should have a Row for each RowId and a Column for each ColumnId that exists in the source table
>>The 'cell' value should be the Id for that combination.....
>
>
>Got this - following the example on
http://msdn.microsoft.com/en-us/library/ms177410(SQL.90).aspx>
>It returns the result you're after - but I'm not exactly sure why
>
>
>declare @tmp Table
> ( Id int,
> ColumnId int,
> RowId Int
> )
>
>insert into @tmp
>select 1, 1, 1
>union all
>select 2, 1, 2
>union all
>select 3, 2, 1
>union all
>select 4, 2, 2
>
>
>
>
>/*
>Row, Column1 Column2
>1 1 3
>2 2 4
>*/
>select RowId, [1], [2], [3], [4]
> from (
> select Id, RowId, ColumnId from @tmp
> ) as SourceTable
> pivot
> (
> max(Id)
> for ColumnId in ([1], [2], [3], [4] )
> ) as PivotTable
> order by RowId
>
>
>
>
>1 1 3 NULL NULL
>2 2 4 NULL NULL
>
Maybe it wasn't a good idea to start this thread usings ints in the example. AFAICS the real problem is that PIVOT needs an aggregating function and you can't do that with a uniqueidentifier.....