准备工作

创建表

 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之行数据转换为列数据的更多相关文章

  1. Mysql 行数据转换为列数据

    现有如下表: 需要统计手机所有售卖价格,显示为如下表: 需要使用group_concat对price进行处理,并且去重重复价格 sql如下: select type,group_concat(DIST ...

  2. 妙用Excel数据透视表和透视图向导,将二维数据转换为一维数据

    项目中,每年都会有各种经销商的各种产品目标数据导入,经销商和产品过多,手工操作过于单调和复杂.那有没有一种方式可以将复杂的二维数据转换为一维数据呢? 有,强大的Excel就支持此功能. 常用Excel ...

  3. 使用python将mysql数据库的数据转换为json数据

    由于产品运营部需要采用第三方个推平台,来推送消息.如果手动一个个键入字段和字段值,容易出错,且非常繁琐,需要将mysql的数据转换为json数据,直接复制即可. 本文将涉及到如何使用Python访问M ...

  4. DTU(用于将串口数据转换为IP数据或将IP数据转换为串口数据通过无线通信网络进行传送的无线终端设备)

    DTU (Data Transfer unit),是专门用于将串口数据转换为IP数据或将IP数据转换为串口数据通过无线通信网络进行传送的无线终端设备.DTU广泛应用于气象.水文水利.地质等行业.

  5. mssql sqlserver 将逗号分隔的一列数据转换为多列数据的方法分享

    转自:http://www.maomao365.com/?p=10278  摘要: 下文讲述sqlserver中将使用逗号组合的单列数据,分隔为多列数据的方法 实验环境:sql server 2012 ...

  6. ArcMAP中Excel数据转换为shp数据

    参考百度知道:http://jingyan.baidu.com/article/f7ff0bfc1cf22c2e26bb138d.html 将数据库中带有X.Y坐标的二维表转换为空间点数据:从数据中将 ...

  7. Sqlserver将表中某列数据以符号分成多行

    WITH testtb2 AS ( UNION ALL ) ) ) ) ) PERCENT SUBSTRING(VisitorCard, STA - LENS, LENS) AS OrderReque ...

  8. jQqery EasyUI dategrid行中多列数据的可编辑操作

    最近的项目中需要在前台dategrid列表中直接修改某些列的数据,并且修改后的数据需要不通过后台而自动更新在列表中. 带着这一问题开始寻找实现的思路,首先想到的就是去jQqery EasyUI官网找例 ...

  9. 【Python】Python 读取csv的某行或某列数据

    Python 读取csv的某行 转载 2016年08月30日 21:01:44 标签: python / csv / 数据   站长用Python写了一个可以提取csv任一列的代码,欢迎使用.Gith ...

随机推荐

  1. grep日志去重

    grep --text ' ERROR '2017.06.08.log | grep '12345678' | grep -Eo 'telephone=.*{11},p'| sort | uniq | ...

  2. 转:Oracle的列转行函数:LISTAGG()

    先看示例代码: with temp as( select 'China' nation ,'Guangzhou' city from dual union all select 'China' nat ...

  3. DV型、OV型、EV型证书的主要区别

    DV型和OV型证书的区别 DV和OV型证书最大的差别是:DV型证书不包含企业名称信息:而OV型证书包含企业名称信息,以下是两者差别对比表:    DV OV  包含企业名称信息 否 是  验证公司名称 ...

  4. linux命令提示符解析

    linux命令提示符 命令窗口中[root@chengfeng ~]# 表示什么意思?       root        当前登录终端的用户      chengfeng     主机名(/etc/ ...

  5. Skyshop.代码解析

    MarmosetInput.cginc: Input结构定义: struct Input { #if defined(MARMO_PACKED_UV) || defined(MARMO_PACKED_ ...

  6. 第24课经典问题(中)-----关于const对象的疑问

    关于const对象的疑问const关键字能否修饰类的对象?如果可以,有什么特性?const关键字能够修饰对象const修饰的对象为只读对象只读对象的成员变量不允许被改变.(对象是只读的,成员变量不允许 ...

  7. windows 7系统下查看被占用的端口并解除占用

    运行输入cmd,在命令行输入 netstat -ano 查找所占用端口所在的行,如图本例子被占用端口为9999,记住对应的pid 然后输入 tasklist|findstr pid(此处为9528) ...

  8. 重拾算法之复杂度分析(大O表示法)

    .katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...

  9. 实现一个new操作符

    new 操作符做了这些事: 1.它创建了一个全新的对象: 2.它会被执行[[Prototype]](也就是__proto__)链接: 3.它使this指向新创建的对象: 4.通过new创建的每个对象最 ...

  10. Tiling Terrace CodeForces - 1252J(dp、贪心)

    Tiling Terrace \[ Time Limit: 1000 ms\quad Memory Limit: 262144 kB \] 题意 给出一个字符串 \(s\),每次可以选择三种类型来获得 ...