CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
IBM Rational 系统开发最佳实践工具包 WebSphere MQ 最佳实践 TOP 15
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

急!行列转换问题

楼主wds1728(wds)2006-03-08 10:19:29 在 MS-SQL Server / 基础类 提问

我现在有如下表:  
  班级           语文           数学           英语    
  1班 80.6 86.0 82.5  
  2班 80.3 83.5 79.0  
  3班 84.5 91.0 87.5  
   
  行列转换成:  
                    1班             2班             3班        
  语文           80.6           80.3           84.5  
  数学           86.0           83.5           91.0  
  英语           82.5           79.0           87.5  
   
  应该怎么做?  
  谢谢 问题点数:50、回复次数:7Top

1 楼libin_ftsafe(子陌红尘:TS for Banking Card)回复于 2006-03-08 10:39:27 得分 50

--生成测试数据  
  create   table   test1(A   varchar(20),b   int,c   int,d   int,e   int)  
  insert   into   test1   select   'x',1,2   ,3   ,4  
  insert   into   test1   select   'y',5,6   ,7   ,8  
  insert   into   test1   select   'z',9,10,11,12  
   
   
  --生成中间数据表  
  declare   @s   varchar(8000)  
  set   @s='create   table   test2(a   varchar(20)'  
  select   @s=@s+','+A+'   varchar(10)'   from   test1  
  set   @s=@s+')'  
  exec(@s)  
   
  --借助中间表实现行列转换  
  declare   @name   varchar(20)  
   
  declare   t_cursor   cursor   for    
  select   name   from   syscolumns    
  where   id=object_id('test1')   and   colid>1   order   by   colid  
   
  open   t_cursor  
   
  fetch   next   from   t_cursor   into   @name  
   
  while   @@fetch_status=0  
  begin  
          exec('select   '+@name+'   as   t   into   test3   from   test1')  
          set   @s='insert   into   test2   select   '''+@name+''''  
          select   @s=@s+','''+rtrim(t)+''''   from   test3  
          exec(@s)  
          exec('drop   table   test3')  
          fetch   next   from   t_cursor   into   @name  
  end  
  close   t_cursor  
  deallocate   t_cursor  
   
   
  --查看行列互换处理结果  
  select   *   from   test2  
   
  /*  
  a         x         y         z  
  ----   ----   ----   ----    
  b         1         5         9  
  c         2         6         10  
  d         3         7         11  
  e         4         8         12  
  */  
   
   
  --删除测试数据  
  drop   table   test1,test2  
  Top

2 楼lsqkeke(可可)回复于 2006-03-08 10:42:21 得分 0

select   '2班', 80.3 ,83.5 ,79.0     union   all  
  select   '3班', 84.5 ,91.0 ,87.5  
  select   *   from   @t  
   
  select   '语文',  
                '1班'=(select   语文   from   @t   where   班级='1班'),  
                '2班'=(select   语文   from   @t   where   班级='2班'),  
                '3班'=(select   语文   from   @t   where   班级='3班')  
  from   @t  
  union  
  select   '数学',  
                '1班'=(select   数学   from   @t   where   班级='1班'),  
                '2班'=(select   数学   from   @t   where   班级='2班'),  
                '3班'=(select   数学   from   @t   where   班级='3班')  
  from   @t  
  union    
  select   '英语',  
                '1班'=(select   英语   from   @t   where   班级='1班'),  
                '2班'=(select   英语   from   @t   where   班级='2班'),  
                '3班'=(select   英语   from   @t   where   班级='3班')  
  from   @tTop

3 楼libin_ftsafe(子陌红尘:TS for Banking Card)回复于 2006-03-08 10:42:52 得分 0

--生成测试数据  
  create   table   test1(班级   varchar(20),语文   numeric(5,1),数学   numeric(5,1),英语   numeric(5,1))                                
  insert   into   test1   select   '1班',80.6,86.0,82.5  
  insert   into   test1   select   '2班',80.3,83.5,79.0  
  insert   into   test1   select   '3班',84.5,91.0,87.5  
   
   
  --生成中间数据表  
  declare   @s   varchar(8000)  
  set   @s='create   table   test2(科目   varchar(20)'  
  select   @s=@s+',['+班级+']   varchar(10)'   from   test1  
  set   @s=@s+')'  
  exec(@s)  
   
  --借助中间表实现行列转换  
  declare   @name   varchar(20)  
   
  declare   t_cursor   cursor   for    
  select   name   from   syscolumns    
  where   id=object_id('test1')   and   colid>1   order   by   colid  
   
  open   t_cursor  
   
  fetch   next   from   t_cursor   into   @name  
   
  while   @@fetch_status=0  
  begin  
          exec('select   '+@name+'   as   t   into   test3   from   test1')  
          set   @s='insert   into   test2   select   '''+@name+''''  
          select   @s=@s+','''+rtrim(t)+''''   from   test3  
          exec(@s)  
          exec('drop   table   test3')  
          fetch   next   from   t_cursor   into   @name  
  end  
  close   t_cursor  
  deallocate   t_cursor  
   
   
  --查看行列互换处理结果  
  select   *   from   test2  
   
  /*  
  科目           1班             2班             3班  
  -------     --------   --------   --------    
  语文           80.6           80.3           84.5  
  数学           86.0           83.5           91.0  
  英语           82.5           79.0           87.5  
  */  
   
   
  --删除测试数据  
  drop   table   test1,test2  
  Top

4 楼lsqkeke(可可)回复于 2006-03-08 10:43:27 得分 0

declare   @t   table(班级   varchar(10),语文   decimal(5,1)   ,   数学   decimal(5,1)   ,英语   decimal(5,1)   )  
  insert   @T  
  select   '1班', 80.6 ,86.0 ,82.5     union   all  
  select   '2班', 80.3 ,83.5 ,79.0     union   all  
  select   '3班', 84.5 ,91.0 ,87.5  
  select   *   from   @t  
   
  select   '语文',  
                '1班'=(select   语文   from   @t   where   班级='1班'),  
                '2班'=(select   语文   from   @t   where   班级='2班'),  
                '3班'=(select   语文   from   @t   where   班级='3班')  
  from   @t  
  union  
  select   '数学',  
                '1班'=(select   数学   from   @t   where   班级='1班'),  
                '2班'=(select   数学   from   @t   where   班级='2班'),  
                '3班'=(select   数学   from   @t   where   班级='3班')  
  from   @t  
  union    
  select   '英语',  
                '1班'=(select   英语   from   @t   where   班级='1班'),  
                '2班'=(select   英语   from   @t   where   班级='2班'),  
                '3班'=(select   英语   from   @t   where   班级='3班')  
  from   @t  
   
  接果:  
                    1班             2班             3班        
  语文           80.6           80.3           84.5  
  数学           86.0           83.5           91.0  
  英语           82.5           79.0           87.5Top

5 楼Lgr_Annie(Annie)回复于 2006-03-08 10:54:37 得分 0

表T1字段:(班级、语文、数学、英语)  
  表T2字段:(学科、1班、2班、3班   )  
   
  先建视图V1:(学科、1班、2班、3班   )  
  select   '语文'   as   学科,  
  case   班级   when   '1班'   then   语文   else   0   end   as   1班,  
  case   班级   when   '2班'   then   语文   else   0   end   as   2班,  
  case   班级   when   '3班'   then   语文   else   0   end   as   3班    
  from   t1    
  union   all    
  select   '数学'   as   学科,  
  case   班级   when   '1班'   then   数学   else   0   end   as   1班,  
  case   班级   when   '2班'   then   数学   else   0   end   as   2班,  
  case   班级   when   '3班'   then   数学   else   0   end   as   3班    
  from   t1    
  union   all    
  select   '英语'   as   学科,  
  case   班级   when   '1班'   then   英语   else   0   end   as   1班,  
  case   班级   when   '2班'   then   英语   else   0   end   as   2班,  
  case   班级   when   '3班'   then   英语   else   0   end   as   3班    
  from   t1    
   
  结果集:  
  select   学科、sum(1班)、sum(2班)、sum(3班)   from   V1   group   by   学科  
  Top

6 楼wgsasd311(自强不息)回复于 2006-03-08 10:54:46 得分 0

create   table   tb(班级   varchar(20),语文   money,   数学   money,英语   money)  
  insert   into   tb  
  select   '1班',80.6,86.0,82.5   union   all  
  select   '2班',80.3,83.5,79.0   union   all  
  select   '3班',84.5,91.0,87.5  
  --test  
  declare   @s1   varchar(8000),@s2   varchar(8000),@s3   varchar(8000)  
  set   @s1='select   ''语文''   as   科目'  
  select   @s1=@s1+',['+班级+']=max(case   when   班级='''+班级+'''   then   语文   end   )'   from   tb   group   by   班级  
  set   @s1=@s1+'   from   tb   '  
   
  set   @s2='select   ''数学''   as   科目'  
  select   @s2=@s2+',['+班级+']=max(case   when   班级='''+班级+'''   then   数学   end   )'   from   tb   group   by   班级  
  set   @s2=@s2+'   from   tb   '  
   
  set   @s3='select   ''英语''   as   科目'  
  select   @s3=@s3+',['+班级+']=max(case   when   班级='''+班级+'''   then   英语   end   )'   from   tb   group   by   班级  
  set   @s3=@s3+'   from   tb   '  
  exec(@s1+'   union     '+@s2+'   union   '+@s3)  
   
  go  
  drop   table   tb  
  Top

7 楼mislrb(上班看看早报,上上CSDN,下班看看电影)回复于 2006-03-08 11:04:29 得分 0

create   table   t_a(班级   varchar(10),语文   decimal(5,1)   ,   数学   decimal(5,1)   ,英语   decimal(5,1)   )  
  insert   t_a  
  select   '1班', 80.6 ,86.0 ,82.5     union   all  
  select   '2班', 80.3 ,83.5 ,79.0     union   all  
  select   '3班', 84.5 ,91.0 ,87.5  
   
  declare   @s1   varchar(8000),@s2   varchar(8000),@s3   varchar(8000)  
  select   @s1='',@s2='',@s3=''  
  select   @s3=@s3+',['+班级+']=isnull(max(case   when   班级='''+班级+'''   then   语文   end),0)'   from   t_a  
  select   @s1='select   科目=''语文'''+@s3+   '   from   t_a   union   all   ',@s3=''  
  select   @s3=@s3+',['+班级+']=isnull(max(case   when   班级='''+班级+'''   then   数学   end),0)'   from   t_a  
  select   @s2='select   科目=''数学'''+@s3+   '   from   t_a   union   all   ',@s3=''  
  select   @s3=@s3+',['+班级+']=isnull(max(case   when   班级='''+班级+'''   then   英语   end),0)'   from   t_a  
  exec(@s1+@s2+'   select   科目=''英语'''+@s3+   '   from   t_a')  
   
  drop   table   t_a  
   
  --结果  
  /*  
  科目       1班             2班             3班              
  ----   -------   -------   -------    
  语文       80.6         80.3         84.5  
  数学       86.0         83.5         91.0  
  英语       82.5         79.0         87.5  
  */Top

相关问题

  • 行列转换
  • 行列转换
  • 行列转换
  • 急,复杂的行列转换问题
  • 高分请教:数据记录的行列转换(着急)
  • 急求:行列转换语句,请赐教。。。
  • 高难度:行列转换?
  • 行列转换的问题
  • 表的行列转换
  • 行列转换问题

关键词

  • 行列
  • varchar
  • cursor
  • exec
  • insert
  • test
  • declare
  • select
  • set

得分解答快速导航

  • 帖主:wds1728
  • libin_ftsafe

相关链接

  • SQL Server类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo