不是 SSRS 报告方面的专家,但可以在 SQL 中完成实际的数据透视。下面的解决方案使用Common Table Expression's (CTE's) 和pivot 关键字。该解决方案不是最动态的解决方案,但它涵盖了您的示例数据。
-- create sample data
declare @data table
(
Account nvarchar(15),
SalesPerson nvarchar(15),
Cost int,
Product nvarchar(15),
ExpectedCloseDate date,
ActualCloseDate date
);
insert into @data (Account, SalesPerson, Cost, Product, ExpectedCloseDate, ActualCloseDate) values
('ABC House', 'John Doe', 120, 'Blocks', '2020-02-03', '2020-02-03'),
('Pizza House', 'Jose Guava', 10, 'Boxes', '2020-04-04', '2020-04-24'),
('The Hanger', 'John Doe', 10, 'Wings', '2020-02-03', '2020-06-24'),
('The Store', 'John Doe', 10, 'Catnip', '2020-02-03', null ),
('The Store', 'Jose Guava', 15, 'Boxes', '2020-03-04', '2020-05-24'),
('WangTai', 'Pete Ringer', 20, 'Lettuce', '2020-04-01', '2020-04-04'),
('WangTai', 'Pete Ringer', 22, 'Paper Plates', '2020-05-01', null ),
('Woolies', 'Pete Ringer', 20, 'Catnip', '2020-03-04', '2020-03-04');
解决方案概述:
- 将日期转换为月份。
- 调整预期成本。
- 以实际成本为中心。
- 将所有内容结合在一起。
解决方案:
-- solution
with dataMonths as
(
select d.Account,
d.SalesPerson,
d.Product,
d.Cost,
month(d.ExpectedCloseDate) as 'ExpectedMonth',
month(d.ActualCloseDate) as 'ActualMonth'
from @data d
),
dataExpected as
(
select p.Account,
p.SalesPerson,
p.Product,
[2] as 'ExpFeb20',
[3] as 'ExpMar20',
[4] as 'ExpApr20',
[5] as 'ExpMay20',
[6] as 'ExpJun20',
[7] as 'ExpJul20'
from dataMonths dm
pivot ( sum(dm.Cost) for ExpectedMonth in ([2], [3], [4], [5], [6], [7]) ) p
),
dataActual as
(
select p.Account,
p.SalesPerson,
p.Product,
[2] as 'ActFeb20',
[3] as 'ActMar20',
[4] as 'ActApr20',
[5] as 'ActMay20',
[6] as 'ActJun20',
[7] as 'ActJul20'
from dataMonths dm
pivot ( sum(dm.Cost) for ActualMonth in ([2], [3], [4], [5], [6], [7]) ) p
)
select dm.Account,
dm.SalesPerson,
dm.Product,
de.ExpFeb20,
da.ActFeb20,
de.ExpMar20,
da.ActMar20,
de.ExpApr20,
da.ActApr20,
de.ExpMay20,
da.ActMay20,
de.ExpJun20,
da.ActJun20,
de.ExpJul20,
da.ActJul20
from dataMonths dm
join dataExpected de
on de.Account = dm.Account
and de.SalesPerson = dm.SalesPerson
and de.Product = dm.Product
join dataActual da
on da.Account = dm.Account
and da.SalesPerson = dm.SalesPerson
and da.Product = dm.Product;
这给了我一些非常接近最终报告的东西:
Account SalesPerson Product ExpFeb20 ActFeb20 ExpMar20 ActMar20 ExpApr20 ActApr20 ExpMay20 ActMay20 ExpJun20 ActJun20 ExpJul20 ActJul20
--------------- --------------- --------------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
ABC House John Doe Blocks 120 120 NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
Pizza House Jose Guava Boxes NULL NULL NULL NULL 10 10 NULL NULL NULL NULL NULL NULL
The Hanger John Doe Wings 10 NULL NULL NULL NULL NULL NULL NULL NULL 10 NULL NULL
The Store John Doe Catnip 10 NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
The Store Jose Guava Boxes NULL NULL 15 NULL NULL NULL NULL 15 NULL NULL NULL NULL
WangTai Pete Ringer Lettuce NULL NULL NULL NULL 20 20 NULL NULL NULL NULL NULL NULL
WangTai Pete Ringer Paper Plates NULL NULL NULL NULL NULL NULL 22 NULL NULL NULL NULL NULL
Woolies Pete Ringer Catnip NULL NULL 20 20 NULL NULL NULL NULL NULL NULL NULL NULL