笔记24 pivot的使用+动态SQL

笔记24 pivot的使用+动态SQL

 1 --pivot的使用+动态SQL
 2 declare @sql varchar(8000)
 3 select @sql = isnull(@sql + '],[' , '') +ltrim(id) from
 4 (
 5      select *,row_number() over (partition by [A] order by (select 1)) as id from dbo.testpivot
 6 ) t group by id
 7 set @sql = '[' + @sql + ']'
 8 PRINT @sql
 9 exec ('select * from (
10 select *,row_number() over (partition by [A] order by (select 1)) as id from dbo.testpivot)
11 a pivot (max(B) for id in (' + @sql + ')) b')

 

posted @ 2013-08-02 21:48 桦仔 阅读(...) 评论(...) 编辑 收藏