SQLServer之行数据转换为列数据
准备工作
创建表
use [test1]
go create table [dbo].[student](
[id] [int] identity(1,1) not null,
[name] [nvarchar](50) null,
[project] [nvarchar](50) null,
[score] [int] null,
constraint [pk_student] primary key clustered
(
[id] asc
)with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary]
) on [primary]
go
插入数据
insert into test1.dbo.student(name,project,score)
values('张三','android',''),
('张三','ios',''),
('张三','html5',''),
('张三','.net',''),
('李四','android',''),
('李四','ios',''),
('李四','html5',''),
('李四','.net','');
使用Case When和聚合函数进行行专列
语法
select column_name,
<aggregation function>(<case when expression>)
from database.schema.table
group by column_name
语法解析
column_name
数据列列名
aggregation function
聚合函数,常见的有:sum,max,min,avg,count等。
case when expression
case when表达式
示例
select name,
max(case project when 'android' then score end) as '安卓',
max(case project when 'ios' then score end) as '苹果',
max(case project when 'html5' then score end) as 'html5',
max(case project when '.net' then score end) as '.net'
from [test1].[dbo].[student]
group by name
示例结果
转换前

转换后

使用PIVOT进行行专列
PIVOT通过将表达式中一列中的唯一值转换为输出中的多个列来旋转表值表达式。并PIVOT在最终输出中需要的任何剩余列值上运行聚合,PIVOT提供比一系列复杂的SELECT...CASE语句指定的语法更为简单和可读的语法,PIVOT执行聚合并将可能的多行合并到输出中的单个行中。
语法
select <non-pivoted column>,
[first pivoted column] as <column name>,
[second pivoted column] as <column name>,
...
[last pivoted column] as <column name>
from
(<select query that produces the data>)
as <alias for the source query>
pivot
(
<aggregation function>(<column being aggregated>)
for
[<column that contains the values that will become column headers>]
in ( [first pivoted column], [second pivoted column],
... [last pivoted column])
) as <alias for the pivot table>
<optional order by clause>;
语法解析
<non-pivoted column>
非聚合列。
[first pivoted column]
第一列列名。
[second pivoted column]
第二列列名。
[last pivoted column]
最后一列列名。
<select query that produces the data>
数据子表。
<alias for the source query>
表别名。
<aggregation function>
聚合函数。
<column being aggregated>
聚合函数列,用于输出值列,最终输出中返回的列(称为分组列)将对其进行分组。
[<column that contains the values that will become column headers>]
转换列,此列返回的唯一值将成为最终结果集中的字段。
[first pivoted column], [second pivoted column], ... [last pivoted column]
数据行中每一行行要转换的列名。
<optional order by clause>
排序规则。
示例
select b.Name,b.[android],b.[ios],b.[html5],b.[.net]
from
(select Name,Project,Score from [test1].[dbo].[student])
as a
pivot
(
max(Score)
for Project in ([android],[ios],[html5],[.net])
)
as b
order by b.name desc
示例结果
转换前

转换后

注意事项
1、如果输出列名不能在表转换列中,则不会执行任何计算。
2、输出的所有列的列名的数据类型必须一致。
SQLServer之行数据转换为列数据的更多相关文章
- Mysql 行数据转换为列数据
现有如下表: 需要统计手机所有售卖价格,显示为如下表: 需要使用group_concat对price进行处理,并且去重重复价格 sql如下: select type,group_concat(DIST ...
- 妙用Excel数据透视表和透视图向导,将二维数据转换为一维数据
项目中,每年都会有各种经销商的各种产品目标数据导入,经销商和产品过多,手工操作过于单调和复杂.那有没有一种方式可以将复杂的二维数据转换为一维数据呢? 有,强大的Excel就支持此功能. 常用Excel ...
- 使用python将mysql数据库的数据转换为json数据
由于产品运营部需要采用第三方个推平台,来推送消息.如果手动一个个键入字段和字段值,容易出错,且非常繁琐,需要将mysql的数据转换为json数据,直接复制即可. 本文将涉及到如何使用Python访问M ...
- DTU(用于将串口数据转换为IP数据或将IP数据转换为串口数据通过无线通信网络进行传送的无线终端设备)
DTU (Data Transfer unit),是专门用于将串口数据转换为IP数据或将IP数据转换为串口数据通过无线通信网络进行传送的无线终端设备.DTU广泛应用于气象.水文水利.地质等行业.
- mssql sqlserver 将逗号分隔的一列数据转换为多列数据的方法分享
转自:http://www.maomao365.com/?p=10278 摘要: 下文讲述sqlserver中将使用逗号组合的单列数据,分隔为多列数据的方法 实验环境:sql server 2012 ...
- ArcMAP中Excel数据转换为shp数据
参考百度知道:http://jingyan.baidu.com/article/f7ff0bfc1cf22c2e26bb138d.html 将数据库中带有X.Y坐标的二维表转换为空间点数据:从数据中将 ...
- Sqlserver将表中某列数据以符号分成多行
WITH testtb2 AS ( UNION ALL ) ) ) ) ) PERCENT SUBSTRING(VisitorCard, STA - LENS, LENS) AS OrderReque ...
- jQqery EasyUI dategrid行中多列数据的可编辑操作
最近的项目中需要在前台dategrid列表中直接修改某些列的数据,并且修改后的数据需要不通过后台而自动更新在列表中. 带着这一问题开始寻找实现的思路,首先想到的就是去jQqery EasyUI官网找例 ...
- 【Python】Python 读取csv的某行或某列数据
Python 读取csv的某行 转载 2016年08月30日 21:01:44 标签: python / csv / 数据 站长用Python写了一个可以提取csv任一列的代码,欢迎使用.Gith ...
随机推荐
- jmeter连接并使用mysql数据
一.下载数据库驱动,放至D:\apache-jmeter-2.13\lib\ext目录下 二.打开jmeter,右键添加->配置文件->JDBC Connection Configurat ...
- emoji表情多终端适配解决方案
emoji表情多终端适配解决方案 emoji,也叫 颜表情 是日本在无线通信中所使用的视觉情感符号,由栗田穰崇(Shigetaka Kurita)创作,并在日本网络及手机用户中流行.自苹果公司发布的i ...
- yum 安装apache php mysql
安装: yum install -y httpd php 查看版本:. rpm -qa httpd php httpd-2.2.15-54.el6.centos.x86_64 php-5.3.3-48 ...
- CF1045B Space Isaac
原题链接 DOWNLOAD AS PDF 题目大意 \(0\sim m-1\)的数被分成两个集合,你可以分别从两个集合中取一个数相加并对\(m\)取模,求一不能构造出的数. 题解 感觉如果\(\col ...
- Java8——Lambda表达式
/* * 一.Lambda 表达式的基础语法:Java8中引入了一个新的操作符 "->" 该操作符称为箭头操作符或 Lambda 操作符 * 箭头操作符将 Lambda 表达 ...
- OSI网络七层模型、TCP/IP 模型(四)
OSI 是 Open System Interconnection 的缩写,译为“开放式系统互联”. OSI 模型把网络通信的工作分为 7 层,从下到上分别是物理层.数据链路层.网络层.传输层.会话层 ...
- LeetCode 153. Find Minimum in Rotated Sorted Array寻找旋转排序数组中的最小值 (C++)
题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...
- USACO Cow Cars
洛谷 P2909 [USACO08OPEN]牛的车Cow Cars https://www.luogu.org/problemnew/show/P2909 JDOJ 2584: USACO 2008 ...
- javascript专题系列--js乱序
乱序的意思想必没有不知道:就是将数组打乱. 听到乱序一般都会想到js的随机函数Math.random(); var values = [1, 2, 3, 4, 5]; values.sort(func ...
- Linux性能优化实战学习笔记:第二十七讲
一.案例环境描述 1.环境准备 2CPU,4GB内存 预先安装docker sysstat工具 2.温馨提示 案例中 Python 应用的核心逻辑比较简单,你可能一眼就能看出问题,但实际生产环境中的源 ...