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 ...
随机推荐
- 【PHP】关于系统性能追踪工具molten
一.简介 关于molten的介绍网上有很多,是一个全链路追踪的工具,Molten可以看做是phptrace的的升级版(流行的php问题定位工具譬如phptrace,xhprof,这些工具可以自行Goo ...
- nginx+tomcat集群时,tomcat参数优化
maxKeepAliveRequests=“1”: nginx动态的转给tomcat,nginx是不能keepalive的,而tomcat端默认开启了keepalive,会等待keepalive的ti ...
- CSS3 边框 border-image
border-image:xx xx xx 是一系列参数的简写,该属性将图片作为边框修饰 border-image-source:url(border.png); 图片url地址 border-ima ...
- Mysql基础知识--概述和索引
一.Mysql概述 MySQL原来隶属于MySQL公司,总部位于瑞典 2008.1.16MySQL被SUN公司收购 2009年SUN公司被Oracle公司收购 常见的软件版本 GA(general A ...
- VIJOS-P1232 核电站问题
VIJOS-P1232 核电站问题 JDOJ 1373 https://neooj.com/oldoj/problem.php?id=1373 题目描述 一个核电站有N个放核物质的坑, ...
- 【Mybatis】多个参数如何写xml和mapper
1:#{0},#{1} 不写parameterType 2:注解 @Param("id")String id 3:Map parameterType="hashma ...
- postgres 字符操作补位,字符切割
补位: ,'); -- 字符切割 并取值: )
- redixdb 基于redis 协议的实时key-value 存储
redixdb 是一个基于redis 协议搞的一个实时key value 处理的轻量级应用,支持多种后端 存储模型. 以下是一个小版的容器镜像(官方的太大了) dockerfile FROM go ...
- 数据结构——链队列(linked queue)
/* linkedQueue.c */ /* 链队列 */ #include <stdio.h> #include <stdlib.h> #include <stdboo ...
- BBS 03day
目录 BBS_03 day: 自定义标签 过滤器: 文章的点赞,点彩功能: 文章的评论功能 transaction用法: 自定义 标签代码展示: BBS_03 day: 自定义标签 过滤器: --&g ...