我有一个SSRS报告,其中一个参数是多选。我需要一个具有聚合行的行组,该行具有总计行,该行根据用户从下拉列表中选择的内容来总计某个数字。例如......如果下拉列表包含A = 4,B = 7,C = 1和D = 3,则假设用户仅选择A和C.分组仅显示包含A和C的行(不将它们分成组,只返回行),总数应为= 5.
答案 0 :(得分:0)
您需要将参数绑定到一个表达式中,该表达式可以使用“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