sqlServer:行列转换之多行转一行
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 ;
sqlServer:行列转换之多行转一行的更多相关文章
- sqlserver行列转换
sqlserver行转列 --创建行转列表及插入数据 create table tb_RowConvertToColumn ( username ) null, course ) null, scor ...
- sqlserver行列转换问题(网上搜集)
(列->行) 一.FOR XML PATH 简单介绍 那么还是首先来介绍一下FOR XML PATH ,假设现在有一张兴趣爱好表(hobby)用来存放兴趣爱好,表结构如 ...
- sqlserver 行列转换
http://www.cnblogs.com/zhangzt/archive/2010/07/29/1787825.html PIVOT用于将列值旋转为列名(即行转列),在SQL Server 200 ...
- SQLServer行列转换PIVOT函数中聚合函数的使用意义及选择
例子:https://blog.csdn.net/wikey_zhang/article/details/76849826 DECLARE @limitDay INT;SET @limitDay = ...
- [转载]SQL Server行列转换实现
可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PIVOT(聚合函数(列) FOR 列 in (…) )AS P 完整语法: table_source PIVOT( 聚合函数(value_ ...
- Hive行列转换
Hive行列转换 1.行转列 (根据主键,进行多行合并一列) 使用函数:concat_ws(‘,’,collect_set(column)) collect_list 不去重 collect_s ...
- PowerBI/Excel - PowerQuery数据转换系列 - 如何将多行的值串联到一行 - 行列转换
Power Query 是做数据转换.数据清洗的利器,不管是在Excel还是PowerBI,如何玩好Power Query是成功建模的必不可少的一步. 今天要get到的一个新技巧:行列转换 如何将多行 ...
- 在Sqlserver下巧用行列转换日期的数据统计
在Sqlserver下巧用行列转换日期的数据统计 前言 在SQLSERVER 中有很多统计函数的基础语法,有使用Group By 或 partition by 后配合Sum,Count(*) 等用法. ...
- sqlserver表分区与调优与行列转换
转自: http://www.cnblogs.com/knowledgesea/p/3696912.html http://www.open-open.com/lib/view/open1418462 ...
随机推荐
- linux添加C#运行环境
linux是不带C#的运行环境的,同样的还有.NET. 有一个叫做Mono的很好用http://www.go-mono.com/,有给docker,而且有环境的选择,要注意. 安好后有给样例的程序,编 ...
- 第60章 设备流交互服务 - Identity Server 4 中文文档(v1.0.0)
该IDeviceFlowInteractionService接口旨在提供用户界面用于在设备流授权期间与IdentityServer通信的服务.它可以从依赖注入系统获得,通常作为构造函数参数注入到Ide ...
- C#_Lamada帮助类
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; us ...
- Nunit的尝试
(ps:没有代码,只有理论) 单元测试 单元测试(Unit Test)的一个测试用例(Test Case)是一小段代码,用于测试一个小的程序功能的行为是否正常,保证开发的功能子项能正确完成并实现其基本 ...
- X级联动
前端数据 @{ ViewBag.Title = "Index"; } <script src="~/Scripts/jquery-1.10.2.js"&g ...
- [Go] golang互斥锁mutex
1.互斥锁用于在代码上创建一个临界区,保证同一时间只有一个goroutine可以执行这个临界区代码2.Lock()和Unlock()定义临界区 package main import ( " ...
- PHP Composer 依赖管理的用法
1:下载 1.1:方法一: 通过PHP来安装 cd G:\web\es6 curl -sS https://getcomposer.org/installer | php #这个命令会下载compos ...
- 【Spring】Autowired原理及与Resource注解区别
Autowired注解 Autowired顾名思义,表示自动注入,如下是Autowired注解的源代码: @Target({ElementType.CONSTRUCTOR, ElementType.M ...
- 用php输出心形曲线
<?php for($t=0;$t<360;$t++) { $y=2*cos($t)-cos(2*$t); //笛卡尔心形曲线函数 $x=2*sin($t)-sin(2*$t); $x+= ...
- 命令行BASH的基本操作
前面说了,我们要尽量少用GNOME图形界面,而应该以使用BASH命令行为主. SHELL Shell是操作系统内核的壳,因为我们不能直接操作系统的内核Kernel,只能通过Shell去操作,Shell ...