在我们的数据库中,我们有:(X表示不关心)

GID   UID    COST
================================
A   1    100
A   1    x
A   2    200
A   2    x
B   3    330
B   3    x

和需要的样子客户报告:

   UID    COST
================================
[Group - A]
   1    100
   1    
   2    200
   2    
   ---Subtotal: 300
[Group - B]
   3    330
   3    x
   ---Subtotal: 330
                        ======Total:    630

我已经2组在SSRS报告,一个是基团上GID,并且一个是基团上的UID,和我已经尝试许多方法来概括所有的UID的一组GID的第一费用。但没有成功。

如果在Crystal报表这样做,我们可以使用“上组变化公式”来实现它。但在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

ROW_NUMBER函数需要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子句是难以置信inffecient因为它必须调用查询在#Rawdata表的每一行。干得不错,但代价是什么?

如果它在数据的生产水平不会导致性能问题,你可能会好起来的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top