sqlServer:行列转换之多行转一行

摘要:
我记得当我第一次加入项目团队并使用oracle数据库时,我遇到的第一个问题是行列转换。哈哈,这真是一个很好的BI。现在当我使用sqlServer数据库时,我又遇到了它。做好记录以备将来使用,并帮助后来者。

记得在刚进项目组时候,使用oracle数据库,遇到的第一个难题就是行列转换,哈哈,真是菜的一BI,现在使用sqlServer数据库,又遇到了,记录一下,以备后用和帮助后来者。

言归正传:

数据库:sqlServer2008R2 英文版

1.建表:学生表(姓名,学科,成绩)

CREATE TABLE teststudent(
    stuname varchar(50) NULL,
    subjects varchar(50) NULL,
    source int NULL
)
drop table teststudent ;
select * from teststudent ;
delete from teststudent ;

2.准备数据:

insert into teststudent(stuname,subjects,source) values('小明','语文',80);
insert into teststudent(stuname,subjects,source) values('小明','数学',85);
insert into teststudent(stuname,subjects,source) values('小明','英语',90);
insert into teststudent(stuname,subjects,source) values('小红','语文',86);
insert into teststudent(stuname,subjects,source) values('小红','数学',90);
insert into teststudent(stuname,subjects,source) values('小红','英语',85);
insert into teststudent(stuname,subjects,source) values('小亮','语文',99);
insert into teststudent(stuname,subjects,source) values('小亮','数学',99);
insert into teststudent(stuname,subjects,source) values('小亮','英语',100);

3.转换:可以采用max()、sum()两种方式

--方式1:
select stuname ,
MAX(case when subjects = '语文' then source else 0 end ) '语文',
MAX(case when subjects = '数学' then source else 0 end ) '数学',
MAX(case when subjects = '英语' then source else 0 end ) '英语'
from teststudent group by stuname ;

--方式2:
select stuname ,
    SUM(case when subjects = '语文' then source else 0 end) '语文',
    SUM(case when subjects = '数学' then source else 0 end ) '数学',
    SUM(case when subjects = '英语' then source else 0 end ) '英语'
from teststudent group by stuname ;

4.其他待补充

免责声明:文章转载自《sqlServer:行列转换之多行转一行》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Selenium3+python3自动化(四十八)--阿里云centos7上搭建selenium启动chrome浏览器headless无界面模式从壹开始 NetCore 新篇章 ║ Blog.Core 开发社之招募计划书【已完成】下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

python直接打印列表

row = [u'课程', u'语文', u'数学', u'英语']print rowprint str(row).decode('unicode-escape')import jsons=json.dumps(row,ensure_ascii=False)print s output: [u'u8bfeu7a0b', u'u8bedu6587', u'u...

现代科技进步促使微积分学继续向前发展

   大家知道,数学软件包(Mathematica)能使我们“看见”数学函数的细微部分的表现,开阔了人们的眼界,发现了“逐点”定义的函数导数的“病态”表现(违反了人们的直觉)。面对这种局面,我们该怎么办呢?          根据文献记载,1690年,数学家罗必达与贝尔努利(l’Hospital/Bernoulli)在微积分教材里面说:“curves co...

汉语词性对照表[北大标准/中科院标准]

汉语词性对照表[北大标准/中科院标准] 词性编码 词性名称 注 解 Ag 形语素 形容词性语素。形容词代码为 a,语素代码g前面置以A。 a 形容词 取英语形容词 adjective的第1个字母。 ad 副形词 直接作状语的形容词。形容词代码a和副词代码d并在一起。 an 名形词 具有名词功能的形容词。形容词代码 a和名词代码n并在一起...

美国数学会众多教授推荐的本科&研究生代数几何经典书籍教材清单

Undergraduate texts.Bix’sConics and Cubics: A Concrete Introductionto Algebraic Geometry[Bix98] concentrates on the zero loci of second degree (conics) and third degree (cubics) t...

ORACLE 如何查看索引重建进度情况

   在ORACLE数据库中,如果一个比较大的索引在重建过程中耗费时间比较长,那么怎么查看索引重建耗费的时间,以及完成了多少(比例)了呢,我们可以通过V$SESSION_LONGOPS视图来查看索引重建的时间和进度。   官方文档关于V$SESSION_LONGOPS的介绍如下 V$SESSION_LONGOPS This view displays...

Git 命令行的各种退出方式

1.保存并退出: (1)按 Esc 键退出编辑模式,英文模式下输入 :wq ,然后回车(write and quit)。 (2)按 Esc 键退出编辑模式,大写英文模式下输入 ZZ ,然后回车。 2.不保存退出:  按 Esc 键退出编辑模式,英文模式下输入 :q! ,然后回车。   按 Esc 键退出编辑模式,英文模式下输入 :qa! ,然后回车。...