table 行转列的sql详解

所属分类: 数据库 / MsSql 阅读数: 666
收藏 0 赞 0 分享
一、要求
1 创建数据表
CREATE TABLE [dbo].[StuScore](
[stuid] [int] NOT NULL,
[subject] [nvarchar](30) NULL,
[score] [decimal](5, 1) NULL
)
2 插入测试数据
stuid subject score
3 chinese 76.0
3 math 73.0
4 chinese 82.0
5 chinese 66.0
5 math 93.0
6 chinese 67.0
7 math 83.0
8 chinese 77.0
8 math 84.0
3 行转列后的结果
stuid chinese math
3 76.0 73.0
4 82.0 0.0
5 66.0 93.0
6 67.0 0.0
7 0.0 83.0
8 77.0 84.0
二 、分析
1 行转列,一个重点就是怎么样知道有多少列,怎么样创建这些列?我们可以先把这个问题搁置,而假设这些列是已知的。 例如示例数据中,可以先假设subject的数据[chinese,math]是已知的,这样问题就简化了许多
2 当已知了chinese,math后,我们至少要先得到转换后的tabel结构
如下;
select stuid, 0 as chinese, 0 as math from dbo.StuScore
结果如下
stuid chinese math
3 0 0
3 0 0
4 0 0
5 0 0
5 0 0
6 0 0
7 0 0
8 0 0
8 0 0
3 接着就需要往这个数据集中去填充chinese, math的数据
select stuid,
case subject when 'chinese' then score else 0 end as chinese,
case subject when 'math' then score else 0 end as math
from dbo.StuScore
结果如下:
stuid chinese math
3 76.0 0.0
3 0.0 73.0
4 82.0 0.0
5 66.0 0.0
5 0.0 93.0
6 67.0 0.0
7 0.0 83.0
8 77.0 0.0
8 0.0 84.0
4 细心的读者会发现步骤3中的结果与我们想要的已经非常接近了,只需再做一个sum()处理,就OK了
select stuid,
sum(case subject when 'chinese' then score else 0 end ) as chinese,
sum(case subject when 'math' then score else 0 end ) as math
from dbo.StuScore group by stuid
得到的正是我们想要的结果
stuid chinese math
3 76.0 73.0
4 82.0 0.0
5 66.0 93.0
6 67.0 0.0
7 0.0 83.0
8 77.0 84.0
是不是现在就已经完成了呢?答案是否定的。前面我们已经说过,是为了简化问题,在假设已经知道了subject数据的情况下,这么处理的,实际上subject的数据是可变的,未知的,接下来就是要解决这个问题了
5 要获取subject的数据其实很简单
select distinct subject from dbo.StuScore
获取以后怎样得到case subject when 'chinese' then score else 0 end 这种语句?
可以根据subject的值去动态的组sql语句
看下面的一段代码
declare @sql varchar(2000)
set @sql=''
select @sql =@sql+ ',case subject when '''+subject+''' then 1 else 0 end as ' + subject
from (select distinct subject from dbo.StuScore) as sub
print @sql
message打印的信息如下:
,case subject when 'chinese' then 1 else 0 end as chinese,case subject when 'math' then 1 else 0 end as math
6 最后我们就需要将前面步骤综合起来,得到最终的sql
declare @sql varchar(2000)
set @sql='select stuid'
select @sql =@sql+ ',sum(case subject when '''+subject+''' then score else 0 end) as ' + subject
from (select distinct subject from dbo.StuScore) as sub
set @sql=@sql + ' from dbo.StuScore group by stuid'
exec(@sql)
stuid chinese math
3 76.0 73.0
4 82.0 0.0
5 66.0 93.0
6 67.0 0.0
7 0.0 83.0
8 77.0 84.0
至此,整个分析过程和结果就都出来了。
初试写文章, 多包涵,指正。
更多精彩内容其他人还在看

sqlserver中将varchar类型转换为int型再进行排序的方法

sql中把varchar类型转换为int型然后进行排序,如果我们数据库的ID设置为varchar型的 在查询的时候order by id的话
收藏 0 赞 0 分享

在SQL Server中使用SQL语句查询一个存储过程被其它所有的存储过程引用的存储过程名

在项目开发中如果有时修改了一个存储过程,但是如何能够快速的查找到使用了这个存储过程的其它存储过程呢
收藏 0 赞 0 分享

sqlserver bcp(数据导入导出工具)一般用法与命令详解

bcp是SQL Server中负责导入导出数据的一个命令行工具,它是基于DB-Library的,并且能以并行的方式高效地导入导出大批量的数据
收藏 0 赞 0 分享

重命名SQLServer数据库的方法

本文讲解重命名SQLServer 数据库,包括物理文件名、逻辑文件名的改名
收藏 0 赞 0 分享

SQL Server中通过reverse取某个最后一次出现的符号后面的内容(字符串反转)

昨天在项目中遇到了一个非常简单的问题,需要把SQL Server数据库中保存的一段路径地址取出其文件名,但SQL Server又没有现成的方法,最后在网上找到这样的一个方法,原理是先将字符串反转,取出第一个/的位置,从头进行截取后再次反转
收藏 0 赞 0 分享

使用SqlBulkCopy时应注意Sqlserver表中使用缺省值的列

今天,想将以前做的一个程序增加点功能,原本就使用SqlBulkCopy批量、定时的从目录中的txt文件导入数据到Sqlserver中。以前一直都使用正常,但是不知怎的就老是出现一个错误
收藏 0 赞 0 分享

Sqlserver 2000/2005/2008 的收缩日志方法和清理日志方法

讲解一下sql 2005日志怎么清理。一般情况下,SQL数据库的收缩并不能很大程度上减小数据库大小,其主要作用是收缩日志大小,应当定期进行此操作以免数据库日志过大
收藏 0 赞 0 分享

SQL Server 2000 清理日志精品图文教程

SQL Server 2000 数据库日志太大!如何清理SQL Server 2000的日志呢
收藏 0 赞 0 分享

SQL行号排序和分页(SQL查询中插入行号 自定义分页的另类实现)

如何在SQL中对行进行动态编号,加行号这个问题,在数据库查询中,是经典的问题
收藏 0 赞 0 分享

sql分类汇总及Select的自增长脚本

对错误信息进行分类汇总,并实现错误数据的自增长编号
收藏 0 赞 0 分享
查看更多