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