Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Can PIVOT do this ?
Message
 
 
To
18/05/2010 09:27:48
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01464982
Message ID:
01465003
Views:
44
>Got this - following the example on PIVOT
>
>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
>
>
>-- select * from @tmp
>
>/*
>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
>
Your code is the same as
select	RowId, [1], [2], [3], [4]
	from @tmp -- no need for derived table in this case
	pivot
	(
		max(Id)
		for ColumnId in ([1], [2], [3], [4] )
	) as PivotTable 
	order by RowId
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform