Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Can PIVOT do this ?
Message
 
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01464982
Message ID:
01465019
Views:
58
>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.....

Here is dynamic Pivot using your example:
declare @Cols varchar(max), @SQL nvarchar(max), @ColsWithNames varchar(max)

select @Cols = coalesce(@Cols + ',','') + QUOTENAME(ColumnID), 
@ColsWithNames = coalesce(@ColsWithNames + ', ','') + QUOTENAME(ColumnID) + ' as Column' + CAST(Row as varchar(10)) 
 from (select CAST(ColumnID as varchar(36)) as ColumnID, 
ROW_NUMBER() over (order by  CAST(ColumnID as varchar(36))) as Row from test50 group by CAST(ColumnID as varchar(36))) X

--print @Cols

set @SQL = 'select RowID, ' + @ColsWithNames + 
' from (select cast(ID as varchar(36)) as ID, cast(ColumnID as varchar(36)) as ColumnID, RowID from test50) src 
PIVOT (min(ID) FOR ColumnID IN (' + @Cols + ')) pvt'

--print @SQL

execute (@SQL)
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform