经典sql

行转列:  

   

  --测试数据  

  create   table   基础表(姓名   nvarchar(10),课程名   nvarchar(20),成绩   decimal(10,1))  

  insert   基础表      select   '王家喜','计算机基础',69.0  

  union     all       select   '王家喜','邓小平理论',74.0  

  union     all       select   '王家喜','英语(上)',86.0  

  union     all       select   '王家喜','普通逻辑学',91.0  

  union     all       select   '施春林','立法学教程',60.0  

  union     all       select   '施春林','经管原理'     ,73.0  

  union     all       select   '施春林','英语(上)',73.0  

  union     all       select   '施春林','普通逻辑学',90.0  

  go 

 

  --查询  

  declare   @s   varchar(8000),@i   varchar(10)

         

  select   top   1   @s='',@i=count(*)  

  from   基础表   group   by   姓名   order   by   count(*)   desc  

  while   @i>0  

  select   @s=',[课程'+@i+']=max(case   when   id='+@i+'   then   课程名   else   ''''   end),

              [成绩'+@i+']=max(case   when   id='+@i+'   then   成绩   end)'+@s ,@i=@i-1  

  exec(' select   姓名,课程名,成绩,id=0   into   #t   from   基础表   order   by   姓名  

      declare   @i   int,@姓名   varchar(10)  

      update   #t   set   @i=case   when   @姓名=姓名   then   @i+1   else   1   end,id=@i,@姓名=姓名  

      select   姓名'+@s+'   from   #t   group   by   姓名')  

  go  

   

  --删除测试 

  select * from   table_goods

 

  drop   table   基础表 

 

  select * from   table_goods

declare @str varchar(1000)

 

set @str='select id,visit '

 

select @str=@str+',min(case when goods='+ Cast(goods As Varchar) +' then goods_id else Null end) as goods_id'+ Cast(goods As Varchar)

 

+',min(case when goods='+ Cast(goods As Varchar) +' then name else Null end) as goods'+ Cast(goods As Varchar)

 

from table_goods group by goods

 

select @str=@str+' from table_goods group by id,visit order by id,visit'

 

print @str exec(@str)

 

 

select id,visit ,max(case when goods=1 then goods_id else Null end) as goods_id1,

               max(case when goods=1 then name else Null end) as goods1,

               max(case when goods=2 then goods_id else Null end) as goods_id2,

               max(case when goods=2 then name else Null end) as goods2,

             max(case when goods=3 then goods_id else Null end) as goods_id3,

              max(case when goods=3 then name else Null end) as goods3,

               max(case when goods=4 then goods_id else Null end) as goods_id4,

                 max(case when goods=4 then name else Null end) as goods4

from table_goods

group by  id,visit order by id,visit

转载于:https://www.cnblogs.com/yangjie5188/archive/2008/02/16/1070534.html

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值