如何对组中的重复值求和(SSRS 2005)

时间:2010-10-13 15:17:50

标签: reporting-services

如何总结重复值 <* em>(SSRS 2005)。 *

例如。 我的查询返回如下值:

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

但是,当我在Group Footer中总计折扣金额时,我无法得到如下的总值。我得到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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

使用总和(不同的x)

对DForcek42的答案进行了一点简化
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