透视转换:

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;

posted on 2013-11-05 09:56  逝者如斯(乎)  阅读(288)  评论(0编辑  收藏  举报