sqlserver 2000如何计算累计百分比(上一行与下一行相加)

pingjing_82 2013-03-17 11:39:45
请各位帮帮忙!!!!谢谢
红色框以我的区域我已经用sql实现,红色框中的用SQL如何实现呢?
也就相当于当前行的【占比】加上一行的【占比】既得到当前行的【累计百分比】
如下图:

...全文
626 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
pingjing_82 2013-03-18
  • 打赏
  • 举报
回复
谢谢,问题解决了.
  • 打赏
  • 举报
回复
--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

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧