T-SQL基础(7) - 透视,逆透视和分组集
透视转换:
use tempdb;
if object_id('dbo.Orders', 'U') is not null drop table dbo.Orders;
create table dbo.Orders
(
orderid int not null,
orderdate date not null,
empid int not null,
custid varchar(5) not null,
aty int not null,
constraint pk_Orders primary key(orderid)
);
insert into dbo.Orders(orderid, orderdate, empid, custid, qty)
values
(300001, '20070802', 3, 'A', 10),
(100001, '20071224', 2, 'A', 12),
(100005, '20071224', 1, 'B', 20),
(400001, '20080109', 2, 'A', 40),
(100006, '20080118', 1, 'C', 14),
(200001, '20080212', 2, 'B', 12),
(400005, '20090212', 3, 'A', 10),
(200002, '20090216', 1, 'C', 20),
(300003, '20090418', 2, 'B', 15),
(300004, '20070418', 3, 'C', 22),
(300007, '20090907', 3, 'D', 30);
SELECT * FROM dbo.Orders;
select empid, custid, sum(qty) as sumqty
from dbo.Orders
group by empid, custid;
| empid | A | B | C | D |
| 1 | NULL | 20 | 34 | NULL |
| 2 | 52 | 27 | NULL | NULL |
| 3 | 20 | NULL | 22 | 30 |
T-SQL解决方案
select empid,
SUM(case when custid = 'A' then qty end) as A,
SUM(case when custid = 'B' then qty end) as B,
SUM(case when custid = 'C' then qty end) as C,
SUM(case when custid = 'D' then qty end) as D
from dbo.Orders
group by empid;
T-SQL Pivot解决方案
select empid, A, B, C, D
from (select empid, custid, qty from dbo.Orders) as O
pivot(sum(qty) for custid in(A, B, C, D)) as P;
select custid, [1], [2], [3]
from (select empid, custid, qty from dbo.Orders) as O
pivot(sum(qty) for empid in ([1], [2], [3])) as P;
逆透视转换:
if object_id('dbo.EmpCustOrders', 'U') is not null drop table dbo.EmpCustOrders;
select empid, A, B, C, D
into dbo.EmpCustOrders
from (select empid, custid, qty from dbo.Orders) as O
pivot(sum(qty) for custid in(A, B, C, D)) as P;
使用标准T-SQL进行逆透视转换
非常明确的需要实现三个逻辑处理阶段:生成副本,提取元素和删除不相关的交叉。
select * from dbo.EmpCustOrders
cross join (values('A'),('B'),('C'),('D')) as Custs(custid);
select * from dbo.EmpCustOrders
cross join (select 'A' as custid
union all select 'B',
union all select 'C',
union all select 'D') as Custs;
select empid, custid, (case custid
when 'A' then A
when 'B' then B
when 'C' then C
when 'D' then D end) as qty
from dbo.EmpCustOrders
cross join (values('A'),('B'),('C'),('D')) as Custs(custid);
select * from (select empid, custid, (case custid
when 'A' then A
when 'B' then B
when 'C' then C
when 'D' then D end) as qty
from dbo.EmpCustOrders
cross join (values('A'),('B'),('C'),('D')) as Custs(custid)) as O
where qty is not null;
T-SQL unpivot解决方案
select empid, custid, qty
from dbo.EmpCustOrders
unpivot(qty for custid in (A, B, C, D)) as U;
分组集:
select empid, custid, sum(qty) as sumqty
from dbo.Orders
group by empid, custid
union all
select empid, null, sum(qty) as sumqty
from dbo.Orders
group by empid
union all
select null, custid, sum(qty) as sumqty
from dbo.Orders
group by custid
union all
select null, null, sum(qty) as sumqty
from dbo.Orders
使用分组集(grouping sets, cube and rollup)
select empid, custid, sum(qty) as sumqty
from dbo.Orders
group by grouping sets
(
(empid, custid),
(empid),
(custid),
()
);
select empid, custid, sum(qty) as sumqty
from dbo.Orders
group by cube(empid, custid);
select empid, custid, sum(qty) as sumqty
from dbo.Orders
group by empid, custid
with cube;
cube(a, b, c)生成3个输入成员得到的8个可能的分组集;而rollup只生成4个分组集,相当于指定了grouping sets((a,b,c), (a,b), (a), ());
select year(orderdate) as orderyear,
month(orderdate) as ordermonth,
day(orderdate) as orderday,
sum(qty) as sumqty
from dbo.Orders
group by rollup(year(orderdate), month(orderdate), day(orderdate));
select year(orderdate) as orderyear,
month(orderdate) as ordermonth,
day(orderdate) as orderday,
sum(qty) as sumqty
from dbo.Orders
group by year(orderdate), month(orderdate), day(orderdate)
with rollup;
T-SQL基础(7) - 透视,逆透视和分组集的更多相关文章
- sql基础语法复习(二)-- 分组,连接的使用
一.深入学习 group by group by ,分组,顾名思义,把数据按什么来分组,每一组都有什么特点. 1.我们先从最简单的开始: select count(*) from tb1 group ...
- SQL点滴19—T-SQL中的透视和逆透视
原文:SQL点滴19-T-SQL中的透视和逆透视 透视 今天抽一点时间来看看透视和逆透视语句,简单的说就是行列转换.假设一个销售表中存放着产品号,产品折扣,产品价格三个列,每一种产品号可能有多种折扣, ...
- SQL Server进阶(八)查询——开窗函数、四大排名函数、透视数据、逆透视数据
概述 ROW_NUMBER() OVER(PARTITION BY CustId ORDER BY ID DESC) https://www.jb51.net/article/75533.htm 开窗 ...
- SQL SERVER技术内幕之7 透视与逆透视
1.透视转换 透视数据(pivoting)是一种把数据从行的状态旋转为列的状态的处理,在这个过程中可能须要对值进行聚合. 每个透视转换将涉及三个逻辑处理阶段,每个阶段都有相关的元素:分组阶段处理相关的 ...
- PIVOT(透视转换)和UNPIVOT(逆透视转换)
一.原数据状态 二.手动写透视转换1 三.手动写透视转换2 四.PIVOT(透视转换)和UNPIVOT(逆透视转换)详细使用 使用标准SQL进行透视转换和逆视转换 --行列转换 create tabl ...
- T-SQL——数据透视和逆透视
目录 0. 测试数据集及说明 0.1 准备测试数据 0.2 对一维表和二维表理解 1. 透视转换 1.1 使用标准SQL进行数据透视 1.2 使用T-SQL中pivot函数进行数据透视 1.3 关于 ...
- UNPIVOT逆透视以及动态逆透视存储过程
前几天一直练习PIVOT透视,还实现了动态透视的存过程<动态透视表>https://www.cnblogs.com/insus/p/10888277.html 今天练习MS SQL Ser ...
- 《BI那点儿事》数据流转换——逆透视转换
逆透视转换将来自单个记录中多个列的值扩展为单个列中具有同样值的多个记录,使得非规范的数据集成为较规范的版本.例如,每个客户在列出客户名的数据集中各占一行,在该行的各列中显示购买的产品和数量.逆透视转换 ...
- T-SQL中的透视和逆透视
透视 今天抽一点时间来看看透视和逆透视语句,简单的说就是行列转换.假设一个销售表中存放着产品号,产品折扣,产品价格三个列,每一种产品号可能有多种折扣,每一种折扣只对应一个产品价格.下面贴出建表语句和插 ...
随机推荐
- 数字证书, 数字签名, SSL(TLS) , SASL .
因为项目中要用到TLS + SASL 来做安全认证层. 所以看了一些网上的资料, 这里做一个总结. 1. 首先推荐几个文章: 数字证书: http://www.cnblogs.com/hyddd/ar ...
- Qt 的内部进程通信机制
Qt 的内部进程通信机制 续欣 (xxin76@hotmail.com), 博士.大学讲师 2004 年 4 月 01 日 Qt 作为一种跨平台的基于 C++ 的 GUI 系统,能够提供给用户构造图形 ...
- 【BASH】自己主动清理rman脚本备份文件
************************************************************************ ****原文:blog.csdn.net/clark_ ...
- 智能手机的工业控制应用方案——SimpleWiFi在工业控制领域应用
智能手机的工业控制应用方案——SimpleWiFi在工业控制领域应用 先上图: 现在的智能控制都是基于微控制器,随着智能的手持终端的普及,基于智能终端的控制就会越来越普遍. WIFI便是其中的一 ...
- 探索Oracle之数据库升级七 11gR2 to 12c 升级完毕后插入PDB
探索Oracle之数据库升级七 11gR2 to 12c 升级完毕后插入PDB 前言: 从Oracle 12c開始,引入了容器数据库的概念,能够实现数据库插拔操作,例如以下图: 如今我们 ...
- nRF Toolbox 1.2 使用AKII的实现,而Becon始终不好使
这几天调试使用nRF51822驱动mpu6050及其数据传输到android中,调试的过程遇到一些困难,apptimer不太会用,然后就參考了下ble_app_hrs的程序,结果成功搞定,demo的价 ...
- C3P0
c3p0详细配置 官方文档 : http://www.mchange.com/projects/c3p0/index.html <c3p0-config> <default-con ...
- (1)cocos2d-x-2.2.4搭建windows开发环境
Cocos2d-x-2.2.4搭建windows环境 软件需求 Windows系统(windows7或之后的系统): cocos2d-x-2.2.4压缩包. python安装包(推荐使用2.7.3版本 ...
- 在Mybatis中使用注解@多个參数查询
@Select("SELECT * FROM wc_homework WHERE organization_id=#{classId} ORDER BY createtime DESC LI ...
- 百度地图AP1
百度地图API的用法 百度地图API演示样例 百度地图API学习总结 <1> <%@ Page Language="C#" Inherits="Syst ...