读书人

sqlserver 2000怎么计算累计百分比(下

发布时间: 2013-03-19 17:22:05 作者: rapoo

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

[解决办法]

--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

读书人网 >SQL Server

热点推荐