>DECLARE @test TABLE (Id uniqueidentifier default newID(), ColumnId int, RowId INT) >INSERT @test ( ColumnId, RowId ) VALUES ( 1,1 ) >INSERT @test ( ColumnId, RowId ) VALUES ( 1,2 ) >INSERT @test ( ColumnId, RowId ) VALUES ( 2,1 ) >INSERT @test ( ColumnId, RowId ) VALUES ( 2,2 ) >SELECT * FROM @test > >SELECT RowID, > cast([1] as uniqueidentifier) as Column1, > cast([2] as uniqueidentifier) as Column2 >from (select cast(Id AS BINARY(16)) AS id, columnId, rowID FROM @test) tmp >pivot >( > max( id ) > for columnID in ([1],[2]) >) AS pvt >>
>PivotTestTable.AsParallel() > .GroupBy (pt => pt.RowId ) > .Select (pt => new { > RowId = pt.Key, > Column1 = pt.SingleOrDefault (p => p.ColumnId==1).Id, > Column2 = pt.SingleOrDefault (p => p.ColumnId==2).Id > }) >Thanks Cetin. That looks great. I would have to build the query on the fly though since the number of columns will not be known in advance. In that respect Naomi's script which dynamically handles the number of columns works well.