我有一张下表
DECLARE @Territory_Month TABLE
(
Territory VARCHAR(20),
[Order Status] VARCHAR(20),
[count Order Number] INT,
[sum Dollar Revenue] FLOAT
);
INSERT INTO @Territory_Month (Territory, [Order Status], [count Order Number], [sum Dollar Revenue])
VALUES ('BENELUX', 'RELEASED', 108, 20363879.11),
('CENTRAL EUROPE', 'RELEASED', 615, 11502633.14),
('EAST EUROPE', 'RELEASED', 292, 4689655.19),
('FRANCE', 'RELEASED', 228, 5333825.69),
('IBERIA', 'RELEASED', 82, 1985003.46),
('ITALY & ISRAEL', 'RELEASED', 211, 3844900.74),
('META', 'RELEASED', 173, 3478765.18),
('NORDICS', 'RELEASED', 132, 4017755.1252),
('SEE', 'RELEASED', 326, 2211346.28),
('UKI', 'RELEASED', 124, 17541413.6),
('BENELUX', 'UNRELEASED', 33, 505935.16),
('CENTRAL EUROPE', 'UNRELEASED', 111, 597457.364505),
('EAST EUROPE', 'UNRELEASED', 96, 2411721.25),
('FRANCE', 'UNRELEASED', 41, 466737.3675),
('IBERIA', 'UNRELEASED', 20, 112696.37),
('ITALY & ISRAEL', 'UNRELEASED', 129, 928122.01),
('META', 'UNRELEASED', 156, 771615.44),
('NORDICS', 'UNRELEASED', 34, 778853.82),
('SEE', 'UNRELEASED', 81, 260567.31),
('UKI', 'UNRELEASED', 115, 6335067.356471);
我想基于此行数据创建数据透视表。结果应如下所示:
这表示每个地区的订单号和美元收入之和除以订单状态。
我尝试使用数据透视表两次,一个用于订单号的总和,一个用于美元的收入,但看起来我不能在同一数据透视表中使用多个列
SELECT
Territory, [RELEASED], [UNRELEASED]
FROM
@Territory_Month
PIVOT
(SUM([count Order Number])
FOR [Order Status] IN ([RELEASED], [UNRELEASED])) piv1
PIVOT
(SUM([sum Dollar Revenue])
FOR [Order Status] IN ([RELEASED], [UNRELEASED])) piv2
ORDER BY
Territory
有什么建议吗?谢谢。
答案 0 :(得分:1)
您的图片不起作用-文本表如此有用的另一个原因。
但是我想你想要这样的东西:
select territory,
sum(case when status = 'RELEASED' then 1 else 0 end) as num_released,
sum(case when status = 'RELEASED' then rev else 0 end) as rev_released,
sum(case when status = 'UNRELEASED' then 1 else 0 end) as num_unreleased,
sum(case when status = 'UNRELEASED' then rev else 0 end) as rev_unreleased
from @Territory_Month tm
group by territory;
Here是db <>小提琴。
请注意,我更改了列名,因此不必进行转义。这样可以更轻松地编写和读取查询。
答案 1 :(得分:1)
通过更改列名和值,您可以将多个列与Pivot一起使用,如下所示
尝试一下
DECLARE @Territory_Month TABLE
(Territory varchar(20), [Order Status] varchar(20), [count Order Number] int,[sum Dollar Revenue] float);
INSERT INTO @Territory_Month
(Territory, [Order Status], [count Order Number], [sum Dollar Revenue])
VALUES
('BENELUX','RELEASED',108,20363879.11),
('CENTRAL EUROPE','RELEASED',615,11502633.14),
('EAST EUROPE','RELEASED',292,4689655.19),
('FRANCE','RELEASED',228,5333825.69),
('IBERIA','RELEASED',82,1985003.46),
('ITALY & ISRAEL','RELEASED',211,3844900.74),
('META','RELEASED',173,3478765.18),
('NORDICS','RELEASED',132,4017755.1252),
('SEE','RELEASED',326,2211346.28),
('UKI','RELEASED',124,17541413.6),
('BENELUX','UNRELEASED',33,505935.16),
('CENTRAL EUROPE','UNRELEASED',111,597457.364505),
('EAST EUROPE','UNRELEASED',96,2411721.25),
('FRANCE','UNRELEASED',41,466737.3675),
('IBERIA','UNRELEASED',20,112696.37),
('ITALY & ISRAEL','UNRELEASED',129,928122.01),
('META','UNRELEASED',156,771615.44),
('NORDICS','UNRELEASED',34,778853.82),
('SEE','UNRELEASED',81,260567.31),
('UKI','UNRELEASED',115,6335067.356471);
Select * From
(
SELECT Territory, [Order Status] OrderStatus1, [Order Status] + '2' OrderStatus2, [count Order Number], [sum Dollar Revenue]
FROM @Territory_Month ) as tbl
PIVOT ( SUM([count Order Number]) FOR [OrderStatus1] in ([RELEASED],[UNRELEASED])) piv1
PIVOT ( SUM([sum Dollar Revenue]) FOR [OrderStatus2] in ([RELEASED2],[UNRELEASED2])) piv2
ORDER BY Territory