Crystal reports 在使用MS SSRS时,如何汇总组中所有子组的第一行的值?

Crystal reports 在使用MS SSRS时,如何汇总组中所有子组的第一行的值?,crystal-reports,ssrs-2008,reporting-services,ssrs-grouping,Crystal Reports,Ssrs 2008,Reporting Services,Ssrs Grouping,在我们的数据库中,我们有:(x表示不在乎) 客户报告要求如下所示: UID COST ================================ [Group - A] 1 100 1 2 200 2 ---Subtotal: 300 [Group - B] 3 330 3 x ---Subtotal: 330 ======Total:

在我们的数据库中,我们有:(x表示不在乎)

客户报告要求如下所示:

   UID    COST
================================
[Group - A]
   1    100
   1    
   2    200
   2    
   ---Subtotal: 300
[Group - B]
   3    330
   3    x
   ---Subtotal: 330
                        ======Total:    630
在SSRS报告中,我有两个组,一个是关于GID的组,另一个是关于UID的组,我尝试了许多方法来总结GID组中UID的所有第一个成本。但是没有成功

如果在Crystal report中这样做,我们可以使用“关于组更改公式”来实现它。但在SSRS中,我找不到正确的方法


请帮忙

您可能需要返回SQL并创建要求和的列

以您的例子:

select 
    GID,
    UID,
    Cost,
    case when row_number()  over(partition by GID,UID ORDER BY GID,UID,Cost) = 1 then Cost else 0 end as firstCostGroup
from
(
    select 'a' as GID, 1 as UID, 100 as Cost
    union
    select 'a', 1, 101
    union
    select 'a', 2, 200
    union
    select 'a', 2, 201
    union
    select 'b', 3, 300
    union
    select 'b', 3, 301 
) as rawdata
行数函数需要SQL 2005或更高版本

SQL 2000的解决方案类似于

drop table #RawData
go
drop table #RawDataFirstRows
GO
create table #RawData
(
id int identity(1,1),
GID varchar(10),
UID int,
Cost int
)

insert into #RawData
    select 'a' as GID, 1 as UID, 100 as Cost 
    union 
    select 'a', 1, 101 
    union 
    select 'a', 2, 200 
    union 
    select 'a', 2, 201 
    union 
    select 'b', 3, 300 
    union 
    select 'b', 3, 301  

create table #RawDataFirstRows
(
id int
)

insert into #RawDataFirstRows
select
rd.id
from #RawData rd
where
rd.id = (select top 1 id from #RawData rw where rd.uid = rw.uid and rd.gid = rw.gid order by rw.gid,rw.uid)


select
rd.GID, rd.UID, rd.Cost, case when rw.id is null then 0 else 1 end as firstCostGroup
from
#RawData rd
left join
#RawDataFirstRows rw on rd.id = rw.id
请注意,where子句中的嵌套查询非常不完善,因为它必须为#Rawdata表中的每一行调用该查询。完成任务,但代价是什么


如果它在数据的生产级别上不会导致性能问题,那么您就可以了。

您可能需要返回SQL并创建要求和的列

以您的例子:

select 
    GID,
    UID,
    Cost,
    case when row_number()  over(partition by GID,UID ORDER BY GID,UID,Cost) = 1 then Cost else 0 end as firstCostGroup
from
(
    select 'a' as GID, 1 as UID, 100 as Cost
    union
    select 'a', 1, 101
    union
    select 'a', 2, 200
    union
    select 'a', 2, 201
    union
    select 'b', 3, 300
    union
    select 'b', 3, 301 
) as rawdata
行数函数需要SQL 2005或更高版本

SQL 2000的解决方案类似于

drop table #RawData
go
drop table #RawDataFirstRows
GO
create table #RawData
(
id int identity(1,1),
GID varchar(10),
UID int,
Cost int
)

insert into #RawData
    select 'a' as GID, 1 as UID, 100 as Cost 
    union 
    select 'a', 1, 101 
    union 
    select 'a', 2, 200 
    union 
    select 'a', 2, 201 
    union 
    select 'b', 3, 300 
    union 
    select 'b', 3, 301  

create table #RawDataFirstRows
(
id int
)

insert into #RawDataFirstRows
select
rd.id
from #RawData rd
where
rd.id = (select top 1 id from #RawData rw where rd.uid = rw.uid and rd.gid = rw.gid order by rw.gid,rw.uid)


select
rd.GID, rd.UID, rd.Cost, case when rw.id is null then 0 else 1 end as firstCostGroup
from
#RawData rd
left join
#RawDataFirstRows rw on rd.id = rw.id
请注意,where子句中的嵌套查询非常不完善,因为它必须为#Rawdata表中的每一行调用该查询。完成任务,但代价是什么


如果它不会在数据的生产级别导致性能问题,您可能会没事。

@Mark,我是Stackoverflow的新手,事实上,我已经尝试对答案进行评分,但系统不允许我这样做…>@马克,我已经告诉Stackoverflow.com我的“马克/投票”情况,现在我能够“接受答案”。非常感谢。@Mark,我是Stackoverflow的新手,的确,我试着给答案打分,但系统不允许我这么做…>@马克,我已经告诉Stackoverflow.com我的“马克/投票”情况,现在我能够“接受答案”。非常感谢。谢谢,这是我找到的方法,但由于我们使用SQL Server 2000,我无法将其应用到公司的环境中。谢谢,这是我找到的方法,但由于我们使用SQL Server 2000,我无法将其应用到公司的环境中。