2017-01-19 1 views
1
私は私の図SSRSでの最悪値の上位3で取得しようと

するためのZでトップ3を取得する方法: 私のコード:ダイアグラムのSSRS:

SELECT * 
    FROM (
    Select top 3 
intervaldate as Datum 
,Name 
,teamname as Team 
,SUM(case when CounterName = 'Blown away' then calculationUnits else 0 end) as Blown 
,Sum(case when CounterName = 'Thrown away' then calculationUnits else 0 end) as Thrown 
,Sum(case when CounterName = 'total' then calculationUnits else 0 end) as Total 

from Counting 
where IntervalDate >= dateadd(day,datediff(day,1,GETDATE()),0) 
    AND IntervalDate < dateadd(day,datediff(day,0,GETDATE()),0) 
and Name in (Select SystemID from tSystemView where SystemViewID = 2)  


group by intervaldate, teamName, Name 
) c 

式:

=Sum(Fields!Blown.Value + Fields!Thrown.Value)/Sum(Fields!Total.Value) * 100 

そして私は、最高から最低 enter image description here

にそれをソートしかし、それは私の右の順に表示されません。 私はそれがトップ3その後、私は他の値を示し、すべての「名前」を選択した場合:

値を持つすべての名称: all Names with value

トップ3: enter image description here

+0

なぜ外部の「選択」ステートメントが必要ですか?何か不足していますか? –

答えて

1

あなたのトップ3の文があるためですあなたの並べ替えがレポート内にある間、SQLで。なしorder by SQLは、上位3つのランダムなレコードを選択します。また、表示されていないSQLがない限り、外側の選択は不要です。 group byの下にorder by <column> descを追加します。

0
with Calcs as 
(
select intervaldate as Datum, 
     Name, 
     TeamName, 
     SUM(case when CounterName = 'Blown away' then calculationUnits else 0 end) as Blown, 
     Sum(case when CounterName = 'Thrown away' then calculationUnits else 0 end) as Thrown, 
     Sum(case when CounterName = 'total' then calculationUnits else 0 end) as Total 
from Counting 
where IntervalDate >= dateadd(day,datediff(day,1,GETDATE()),0) 
AND IntervalDate < dateadd(day,datediff(day,0,GETDATE()),0) 
and Name in (Select SystemID from tSystemView where SystemViewID = 2) 
group by intervaldate, teamName, Name 
) 
select b.* 
from 
(
    select a.*, row_number() over (order by (Blown + Thrown)/Total desc) as R_Ord -- Change between ASC/DESC depending on needs 
    from Calcs a 
) b 
where R_Ord <=3 
+0

「ゼロで除算する」を避けるにはどうすればよいですか? –

+0

'IIF(合計(フィールド合計!値)= 0、0、合計(フィールド! * 100) ' –