2010-10-13 15 views
0

如何對一個組* (SSRS 2005)中的重複值進行求和。 *如何對一個組中的重複值進行求和(SSRS 2005)

例如。 我的查詢返回值如下所示:

CusID Discount Amount 
1  20  1000 
1  20  2000 
1   5  700 
2  15  1500 
2  15  3000 

但是,當我在組頁腳總結優惠金額,我不能得到像下面的總價值。我得到45的CusID 1而不是25。請幫我解決這個問題。謝謝。

CusID Discount Amount 
1  20  1000 
1  20  2000 
1   5  700 
------------------------ 
Total 25  3700 

2  15  1500 
2  15  3000 
------------------------ 
Total 15  4500 
+1

當然對於CusID 1折扣應該是17(按金額加權平均折扣),而不是25? – 2010-10-13 17:04:22

+0

那裏有一些業務邏輯可能需要在報表的.Code部分或SQL中使用自定義函數來處理。確定折扣的邏輯是什麼? – 2010-10-14 16:25:49

+0

假設折扣是%折扣而不是$折扣是正確的嗎? – Craig 2010-10-21 20:45:59

回答

0

沒有你的實際數據看起來像,我只能提供一個基於你提供的數據的代碼示例。

declare @table table (CustID int, Discount int, Amount int) 


    insert into @table (CustID,Discount,Amount) 
    select 1 as CusID,20 as Discount,1000 as Amount 
    union all 
    select 1,20,2000 
    union all 
    select 1,5,700 
    union all 
    select 2,15,1500 
    union all 
    select 2,15,3000 

    select 
     CustID, 
     sum(Discount) as Discount, 
     sum(Amount) as Amount 
    from 
    (
     select 
      CustID, 
      Discount, 
      SUM(Amount) as Amount 
     from @table 
     group by CustID, Discount 
    ) a 
    group by CustID 
0

一個輕微簡化到DForcek42的回答通過使用總和(不同X)

declare @table table (CustID int, Discount int, Amount int) 

insert into @table (CustID,Discount,Amount) 
select 1 as CusID,20 as Discount,1000 as Amount 
union all 
select 1,20,2000 
union all 
select 1,5,700 
union all 
select 2,15,1500 
union all 
select 2,15,3000 

select 
    CustID, 
    sum(distinct Discount), 
    SUM(Amount) as Amount 
from @table 
group by CustID 
相關問題