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中的透视和逆透视
透视 今天抽一点时间来看看透视和逆透视语句,简单的说就是行列转换.假设一个销售表中存放着产品号,产品折扣,产品价格三个列,每一种产品号可能有多种折扣,每一种折扣只对应一个产品价格.下面贴出建表语句和插 ...
随机推荐
- XML实例文档
from: http://www.w3school.com.cn/xpath/xpath_examples.asp XML实例文档 我们将在下面的例子中使用这个 XML 文档: "books ...
- 【UVA】10012 - How Big Is It?(暴力)
使用DFS枚举所有的安排.每次加入后,当一个圆.他的立场是最大的,并已加入了圆环中的所有切线位置前面. 14383635 10012 option=com_onlinejudge&Itemid ...
- 基于Grunt的版本号构建系统新手教程
作者:zhanhailiang 日期:2014-10-12 1. 安装nodejs,npm,grunt-cli.參见<Windows环境下安装nodejs+npm+grunt-cli工具> ...
- [LeetCode] Search for a Range [34]
题目 Given a sorted array of integers, find the starting and ending position of a given target value. ...
- Android学习-各种对话框
在android的编程中,对话框的用处是非常多的,然而,有时对对话框的需求也是不一样的,那么现在,就总结对话框的种类,以及事件的处理形势. 1.简单的对话框 public void showDialo ...
- java 线程关闭小结(转)
首先,要说的是java中没有一种停止线程的方法是绝对安全的.线程的中断Thread.interrput()方法很容易给人一种误会,让人感觉是一个线程使另外一个正在运行的线程停止工作,但实际上inter ...
- Eclipse扩展点实践之添加菜单项(ActionSet方式实现)
ActionSet方式比起Command方式,比较直观,但是功能有限. 首先:新建一个项目,在Extension中添加org.eclipse.ui.actionSets的扩展. 然后,new-> ...
- 收藏的一段关于java大数运算的代码
收藏的一段关于java大数运算的代码: package study_02.number; import java.math.BigDecimal; import java.math.BigIntege ...
- 重温delphi之控制台程序:Hello World!
原文:重温delphi之控制台程序:Hello World! 这二天用c#开发ActiveX时,发现不管怎么弄,c#就是没办法生成ocx的纯正activeX控件,而且还要强迫用户安装巨大的.net f ...
- Nagios+pnp4nagios+rrdtool 安装配置为nagios添加自定义插件(三)
nagios博大精深,可以以shell.perl等语句为nagios写插件,来满足自己监控的需要.本文写mysql中tps.qps的插件,并把收集到的结果以图形形式展现出来,这样输出的结果就有一定的要 ...