首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

行列转换有关问题案例回答

2012-09-14 
行列转换问题案例回答 原论坛贴子地址:点击打开链接http://topic.csdn.net/u/20120817/14/2b37210c-e7cb-4

行列转换问题案例回答

 原论坛贴子地址:点击打开链接    http://topic.csdn.net/u/20120817/14/2b37210c-e7cb-4713-bca3-97eb1f600ca7.html?seed=1450377381&r=79434916#r_79434916             /********************************************************************************                *主题: SQl 2008/2005 论坛问题解答                *说明:本文是个人学习的一些笔记和个人愚见                 *      有很多地方你可能觉得有异议,欢迎一起讨论                                 *作者:Stephenzhou(阿蒙)                 *日期: 2012.08.17           *Mail:szstephenzhou@163.com                 *另外:转载请著名出处。                **********************************************************************************/    create table tb (col1 varchar(20),col2 varchar(20))insert tbselect 'A','ddd' union allselect 'B','asdf' union allselect 'C','dsdf' union allselect 'D','eee' union allselect 'E','geas' union allselect 'F','2' union allselect 'G','5d' union allselect 'H','sad' union allselect 'I','ww' union allselect 'J','23' union allselect 'K','asdfw'   declare @sql varchar(max)declare @qls varchar(8000)select @sql ='select '; select @sql+=' max(case col1 when '''+col1+''' then col2 else null end)['+col1+'] ,' from (select distinct col1 from tb)as aset @sql = @sql + 'from tb  'set @sql=substring(@sql,0,charindex('from',@sql)-1)+substring(@sql,charindex('from',@sql),LEN(@sql))print @sqlexec (@sql)   /*    A                    B                    C                    D                    E                    F                    G                    H                    I                    J                    K-------------------- -------------------- -------------------- -------------------- -------------------- -------------------- -------------------- -------------------- -------------------- -------------------- --------------------ddd                  asdf                 dsdf                 eee                  geas                 2                    5d                   sad                  ww                   23                   asdfw (1 行受影响)  */    --or selectMAX(case col1 when 'A' then col2 else null end ) as 'A',MAX(case col1 when 'B' then col2 else null end ) as 'B',MAX(case col1 when 'C' then col2 else null end ) as 'C',MAX(case col1 when 'D' then col2 else null end ) as 'D'.......  from tb


 *作者:Stephenzhou(阿蒙)     
 *日期: 2012.08.17

 *Mail:szstephenzhou@163.com     
 *另外:转载请著名出处。
 *博客地址:http://blog.csdn.net/szstephenzhou

热点排行