日期:2014-05-17  浏览次数:20659 次

sqlserver 2000如何计算累计百分比(上一行与下一行相加)
请各位帮帮忙!!!!谢谢
红色框以我的区域我已经用sql实现,红色框中的用SQL如何实现呢?
也就相当于当前行的【占比】加上一行的【占比】既得到当前行的【累计百分比】
如下图:


------解决方案--------------------
--sql 2000
declare @tb table(row int identity(1,1),故障总成件 varchar(100),数量 int,占比 float)
insert into @tb select * from tb
select 故障总成件,数量,占比,累计百分比=(select sum(占比) from @tb t2 where t2.row<=t1.row) from @tb t1


--sql 2005
with tc as(
select row=row_number()over(order by getdate()),* from tb
),
cte as(
select *,累计百分比=cast(占比 as decimal(28,3)) from tc where row=1 union all
select t.row,t.故障总成件,t.数量,t.占比,cast(c.累计百分比+t.占比 as decimal(28,3)) from tc t join cte c on t.row=c.row+1
)
select * from cte