对于行列转换的数据,通常也就是在做报表的时候用的比较多,之前也零零散散的看了一些,今天就来总结一下。

先创建一个用于演示的临时表:

create table #temp
(
年份 nvarchar(10) null,
月份 nvarchar(10) null,
数量 int null
) insert into #temp(年份,月份,数量)
select '','','' union
select '','','' union
select '','','' union
select '','','' union
select '','','' union
select '','','' union
select '','','' union
select '','','' union
select '','','' select * from #temp

下面来实现一些需求:

需求一,按年份分组,不同的月份为一列。

-- 按年份分组,不同的月份为一列
select t.年份,
sum(case t.月份 when '' then t.数量 end) '1月份',
sum(case t.月份 when '' then t.数量 end) '2月份',
sum(case t.月份 when '' then t.数量 end) '3月份'
from #temp t
group by t.年份

另外两种方法:

-- 使用左外连接查询
select t.年份,t1.数量 '1月份',t2.数量 '2月份',t3.数量 '3月份' from #temp t
left join (select 年份,数量 from #temp where 月份='') t1 on t.年份=t1.年份
left join (select 年份,数量 from #temp where 月份='') t2 on t.年份=t2.年份
left join (select 年份,数量 from #temp where 月份='') t3 on t.年份=t3.年份
group by t.年份,t1.数量,t2.数量,t3.数量 -- 使用自连接查询
select t.年份,t1.数量 '1月份',t2.数量 '2月份',t3.数量 '3月份'
from #temp t,
(select 年份,数量 from #temp where 月份='') t1,
(select 年份,数量 from #temp where 月份='') t2,
(select 年份,数量 from #temp where 月份='') t3
where t.年份=t1.年份 and t.年份=t2.年份 and t.年份=t3.年份
group by t.年份,t1.数量,t2.数量,t3.数量

返回的结果都是一样的,可以看见这几种方法都是可以实现的(当然,可能还有更多的方法待发掘),不过比起第一种方法,后面这两种方法也太低效了吧,比如一年有12个月份的数据,有个七八年的,那得写多少个子查询、表连接的,而且第一种方法也不是我们想要的。那么就需要用到 Pivot 这种方法了。

Pivot 语法:

table_source    -- 表名称,即数据源

    PIVOT(

    聚合函数(value_column)    -- value_column 要转换为 列值 的列名

    FOR pivot_column        -- pivot_column 指定要转换的列

    IN(<column_list>)        -- column_list 自定义的目标列名
)

因为这里列名不允许指定为数字,真是无语。。。我重建了一个数据结构一模一样的表。

create table #temp
(
Name nvarchar(10) null,
Course nvarchar(10) null,
Score int null
) insert into #temp(Name,Course,Score)
select '小李','语文','' union
select '小李','数学','' union
select '小李','英语','' union
select '小明','语文','' union
select '小明','数学','' union
select '小明','英语','' union
select '小红','语文','' union
select '小红','数学','' union
select '小红','英语','' select * from #temp
go

select Name 姓名,
max(case Course when '语文' then Score end) 语文,
max(case Course when '数学' then Score end) 数学,
max(case Course when '英语' then Score end) 英语,
sum(Score) 课程总分,
cast(avg(Score) as decimal(18,2)) 课程平均分
from #temp
group by Name

使用 Pivot 进行 行转列:

select a.Name 姓名,a.语文,a.数学,a.英语
from #temp
pivot
(
max(Score) -- 指定作为转换的列的值 的列名
for Course -- 指定要转换的列的列名
in(语文,数学,英语) -- 自定义的目标列名,即要转换列的不同的值作为列
)a

select a.Name 姓名,a.语文,a.数学,a.英语,b.SumScore 课程总分,b.AvgScore 课程平均分
from #temp
pivot
(
max(Score) -- 指定作为转换的列的值 的列名
for Course -- 指定要转换的列的列名
in(语文,数学,英语) -- 自定义的目标列名,即要转换列的不同的值作为列
)a,
(
select t.Name,sum(t.Score) SumScore,cast(avg(t.Score) as decimal(18,2)) AvgScore
from #temp t
group by t.Name
)b
where a.Name=b.Name

UnPivot 语法:

table_source    -- 表名称,即数据源

    UNPIVOT(

    value_column    -- value_column 要转换为 行值 的列名

    FOR pivot_column    -- pivot_column 指定要转换为指定的列

    IN(<column_list>)    -- column_list 目标列名
)
create table #temp
(
Name nvarchar(10) null,
Chinese int null,
Math int null,
English int null
) insert into #temp(Name,Chinese,Math,English)
select '小李','','','' union
select '小明','','','' union
select '小红','','','' select * from #temp
go

select t.Name 姓名,t.Course 课程,t.Score 分数 from
(select t.Name,Course='Chinese',Score=Chinese from #temp t
union all
select t.Name,Course='Math',Score=Math from #temp t
union all
select t.Name,Course='English',Score=English from #temp t) t
order by t.Name,t.Course
select t.Name 姓名,t.Course 课程,t.Score 分数 from
(select t.Name,'Chinese' Course,Chinese Score from #temp t
union all
select t.Name,'Math',Math from #temp t
union all
select t.Name,'English',English from #temp t) t
order by t.Name,t.Course

使用 UnPivot 进行 列转行:

select t.Name 姓名,t.Course 课程,t.Score 分数
from #temp
unpivot
(
Score for Course
in(Chinese,Math,English)
)t

SQL Server 使用 Pivot 和 UnPivot 实现行列转换的更多相关文章

  1. SQL server 2005 PIVOT运算符的使用

    原文:SQL server 2005 PIVOT运算符的使用 PIVOT,UNPIVOT运算符是SQL server 2005支持的新功能之一,主要用来实现行到列的转换.本文主要介绍PIVOT运算符的 ...

  2. SQL Server数据库PIVOT函数的使用详解(一)

    http://database.51cto.com/art/201108/285250.htm SQL Server数据库中,PIVOT在帮助中这样描述滴:可以使用 PIVOT 和UNPIVOT 关系 ...

  3. SQL Server里PIVOT运算符的”红颜祸水“

    在今天的文章里我想讨论下SQL Server里一个特别的T-SQL语言结构——自SQL Server 2005引入的PIVOT运算符.我经常引用这个与语言结构是SQL Server里最危险的一个——很 ...

  4. SQL Server将查询出数据进行列转行操作

    在日常的SQL Server数据查询时经常会遇到需要将数据列转换成行的操作,现将自己学习的列转行SQL语句举例如下: --首先查询语句 SELCT * FROM  YXBAK..TBYJKSTEMP ...

  5. SQL Server数据库PIVOT函数的使用详解(二)

    动态的行转列 原理就是 把需要转成列的行拼出来 DECLARE @fieldName VARCHAR(); SET @fieldName=''; SELECT @fieldName = @fieldN ...

  6. 在SQL Server中 获取日期、日期格式转换

    --常用日期转换参数: PRINT CONVERT(varchar, getdate(), 120 ) 2016-07-20 16:09:01 PRINT replace(replace(replac ...

  7. sql server 2005全角与半角字符转换

    CREATE FUNCTION D_ByteExchangeS_Byte(@str NVARCHAR(4000), --要转换的字符串@flag bit              --转换标志,0转换 ...

  8. SQL Server 一些使用小技巧

    1.查询的时候把某一个字段的值拼接成字符串 以下是演示数据. 第一种方式:使用自定义变量 ) SET @Names='' -- 需要先赋值为空字符串,不然结果会是 null SELECT @Names ...

  9. Sql实现行列转换

    从MS Sql Server 2005微软就推出了pivot和unpivot实现行列转换,这极大的方便了我们存储数据和呈现数据.今天就对这两个关键字进行分析,结合实例讲解如何存储数据,如何呈现数据. ...

随机推荐

  1. 1 week110的zookeeper的安装 + zookeeper提供少量数据的存储

    随时查看,zookeeper企业里公认的最新文档版本!       https://archive.apache.org/dist/    下面是在weekend110上的zookeeper的安装 在 ...

  2. 《Programming with Objective-C》第七章 Values and Collections

    1.平台相关的数据类型 These types, like NSInteger and NSUInteger, are defined differently depending on the tar ...

  3. redhat6.4 zabbix3.0.2安装

    zabbix不用说了,很好的服务器监控管理工具,还支持中文哈! 1.添加epel仓库,有更多可用的软件包 rpm -ivh http://download.fedoraproject.org/pub/ ...

  4. 表空间_暂时表空间引起的错误:ora-01652 小例

    原创作品,出自 "深蓝的blog" 博客,深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/46888243 报暂 ...

  5. Spring学习笔记--使用Spring基于Java的配置

    我们需要使用@Component注解来定义一个配置类,在配置类中我们定义Bean: package com.moonlit.myspring; import org.springframework.c ...

  6. 第一个内核模块hello world

    1.源码树的下载和编译(只是研究内核模块的话,应该不需要源码树的) 下载很简单,压缩包解压 编译:make menuconfig make bzImage -j4 参考 2. cd  /usr/src ...

  7. WPS长文档编辑技巧之二:对样式的设置与修改

    目录:       1.使用系统内置样式 2.如何修改样式 3.如何自定义样式 4.在文档使用多级编号 5.结合样式编辑文档大纲 6.利用文档结构图查看大纲结构 正文: 1.使用系统内置样式 在使用样 ...

  8. poj_2486 动态规划

    题目大意 N个节点构成一棵树,每个节点上有一个权重val[i], 从根节点root出发在树上行走,行走的时候只能沿着树枝行进.最多在树上走k步,每第一次到达某个节点j,可以获得val[j]的收益,求从 ...

  9. 利用MFC实现浏览器的定制与扩展(JavaScript与C++交互)

    原文地址:http://www.vckbase.com/document/viewdoc/?id=1486 浏览器的定制与扩展       作者:李汉鹏 下载源代 码  本文分如下章节: 前 言 在 ...

  10. <span>和<div>标签的隐藏和显示切换

    <div class="axb"> <span id="tipStep1" class="fl" >第一步显示< ...