【发布时间】:2013-11-04 20:28:16
【问题描述】:
我有一个 SSRS 报告,其中一个参数是多选。我需要一个带有聚合行的行组,该行有一个总计行,该行仅根据用户从下拉列表中选择的内容来汇总一些数字。例如....如果下拉列表包含 A = 4、B = 7、C = 1 和 D = 3,假设用户仅选择 A 和 C。分组只显示包含 A 和 C 的行(不将它们分成组,只返回 rows )并且总数应该 = 5。
【问题讨论】:
我有一个 SSRS 报告,其中一个参数是多选。我需要一个带有聚合行的行组,该行有一个总计行,该行仅根据用户从下拉列表中选择的内容来汇总一些数字。例如....如果下拉列表包含 A = 4、B = 7、C = 1 和 D = 3,假设用户仅选择 A 和 C。分组只显示包含 A 和 C 的行(不将它们分成组,只返回 rows )并且总数应该 = 5。
【问题讨论】:
您需要将您的参数绑定在一个表达式中,该表达式可以使用“IN”子句对多值参数进行评估。现在使用 SSRS,它处理参数的方式与 SQL 不同,因此我认为您可以使用更少的代码。这是一个直接用 SQL 编写的示例,它会自动提取并运行。
declare @People Table ( personID int identity, person varchar(8));
insert into @People values ('Brett'),('Sean'),('Chad'),('Michael'),('Ray'),('Erik'),('Queyn');
declare @Orders table ( OrderID int identity, PersonID int, Description varchar(32), Amount int);
insert into @Orders values (1, 'Shirt', 20),(1, 'Shoes', 50),(2, 'Shirt', 22),(2, 'Shoes', 52),(3, 'Shirt', 20),(3, 'Shoes', 50),(3, 'Hat', 20),(4, 'Shirt', 20),(5, 'Shirt', 20),(5, 'Pants', 30),
(6, 'Shirt', 20),(6, 'RunningShoes', 70),(7, 'Shirt', 22),(7, 'Shoes', 40),(7, 'Coat', 80)
declare @Ords table
(
value varchar(32)
)
--PLAY with values to see agregation works with different MULTIPLE CHOICES
-- in reality @Ords would be your parameter and the rest of the stuff if just
-- faking a dataset.
insert into @Ords VALUES ('Shirt');
--insert into @Ords VALUES ('Shirt'),('Shoes');
--insert into @Ords VALUES ('Shirt'),('Shoes'),('Hat');
-- simple way when you can expose dataset to join to.
Select
p.personID
, p.person
, sum(case when v.value is not null then 1 end)
, sum(case when v.value is not null then Amount end)
from @People p
left join @Orders o on o.PersonID = p.personID
left join @Ords v on o.Description = v.value
group by p.personID, p.person
order by p.personID
;
-- With SSRS you probably cannot JOIN directly to your parameter(never tried it though, maybe you can)
-- so you need to do an 'IN' expression in a CTE and then aggregate your CTE. You can shorten this
-- in SSRS to be in (@parameterName) instead of (Select value from @ParameterName)
With a as
(
Select
p.person
, p.personID
, case when o.Description in (Select value from @Ords) then 1 end as Cnt
, case when o.Description in (Select value from @Ords) then Amount end as Amount
from @People p
left join @Orders o on o.PersonID = p.personID
)
Select
personID
, person
, count(Cnt)
, sum(Amount)
from a
group by personID, person
order by personID
【讨论】: