2013-11-04 70 views
0

我有一個SSRS報告其中的參數中的一個是多選。我需要一個行組與集聚行有求和僅基於什麼用戶從下拉菜單中選擇某編號的彙總行。例如....如果下拉包含A = 4,B = 7,C = 1,d = 3,讓我們說,用戶選擇A和C只。含A和C(不分離出來成團,剛剛返回行)和總的分組只能說明行應= 5SSRS上字段分組=多值參數

回答

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 
+0

謝謝djangojazz,非常酷的建議,但我也許應該更清楚。我正在尋找SSRS中的功能或操作來完成此功能。我有一個SP,它在報告中提供數據的報告後面運行。我只是想操縱報表設計中的數據。我試過使用= join函數和其他一些東西,但我找不到解決方案。 – Kat

+0

我從來沒有做過,但你可能按一個參數作爲一個SSRS「分組」。然後,只需執行[SUM(DataField)],分組就會自動處理數學部分。我會遠離你將不得不做一個評估,恕我直言,表現遠的表達是不好當聚集。當你必須或直接使用sql時,使用它們,計算列,自定義代碼或分組。我有幾種情況使用自定義表達式來使用百分比分組,我很遺憾這種選擇,因爲它們很慢,需要重寫,我懶得做。 – djangojazz

+0

如果你有一個程序,你可以改變代碼嗎?我在程序中遇到的問題是您正在拿走所有的靈活性並將其放入代碼中。好處是你可以直接從SQL改變你的結果。這取決於你的情況是一種混合的祝福。出於這個原因,我通常使用SSRS的視圖和表函數。速度可能不會像使用proc一樣好,但是我可以更好地控制演示級別。 – djangojazz