table 行转列的sql详解

2016-02-19 09:06 0 1 收藏

下面图老师小编跟大家分享一个简单易学的table 行转列的sql详解教程,get新技能是需要行动的,喜欢的朋友赶紧收藏起来学习下吧!

【 tulaoshi.com - 编程语言 】

一、要求
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
至此,整个分析过程和结果就都出来了。
初试写文章, 多包涵,指正。

来源:https://www.tulaoshi.com/n/20160219/1589524.html

延伸阅读
曾经以为SQL SERVER的触发器只能触发单行,也就是说如果一个delete触发器,如果同时删除多行时,只会对第一条记录触发,后来发现了不是人家SQL SERVER不支持,而是偶脑子笨没发现。 其实inserted和deleted两张内部表存放了所有要插入或要删除的记录,可以用cursor逐次访问里面的每条记录,下面是一个示例,该触发器将要删除的记录转移...
一、SQL Profiler工具简介 SQL Profiler是一个图形界面和一组系统存储过程,其作用如下: 1.图形化监视SQL Server查询; 2.在后台收集查询信息; 3.分析性能; 4.诊断像死锁之类的问题; 5.调试T-SQL语句; 6.模拟重放SQL Server活动; 也可以使用SQL Profiler捕捉在SQL Server实例上执行的活动。这样的活动被称为Profiler跟踪。 ...
《龙腾世纪 审判》war table任务剧情详解攻略 《龙腾世纪:审判》游戏中含有一些主线及支线任务需要玩家们达成,今天给大家带来玩家分享的WAR TABLE任务剧情攻略,一起来看看吧。 其实WAR TABLE 是DAI非常有特点的一个内容, 他集中体现了审判庭如何影响世界的过程,同时里面充满了变量和可能性 。 但是由于目前蹩脚的汉化,已经大量的英文...
(1)二进制数据类型 二进制数据包括 Binary、Varbinary 和 Image Binary 数据类型既可以是固定长度的(Binary),也可以是变长度的。 Binary[(n)] 是 n 位固定的二进制数据。其中,n 的取值范围是从 1 到 8000。其存储窨的大小是 n + 4 个字节。 Varbinary[(n)] 是 n 位变长度的二进制数据。其中,n 的取值范围是从 1 到 8000...
标签: 怀孕
给宝宝起名时如何为五行缺土的宝宝取名?可以参考用起名字五行属土的字组合取,但是起名字五行属土的字都有哪些,分别是什么意思?又可以由这些起名字五行属土的字组合成什么名字?我们来看看起名字五行属土的字详解吧! >>> 2013金蛇宝宝起名大全之5注意事项 五行缺土取名详解 起名字五行属土的字详解 山3土:意为安稳如山、有依靠。...

经验教程

470

收藏

33
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部