sql 中每一列的累加

dkjdkj 2009-06-06 08:50:12
一张表中,数据已经有的数据如下,在执行一个累加后 变成下一张表,累加的规则是,从编号1开始,第一个不变,编号原来数据是8加上编号1的数据8 是等于16,然后在填回编号2,编号3的数据,是编号2+原来编号3的数据再填加回去


编号 完成数
==============================
1 8
2 8
3 4
4 7
5 3
6 3



编号 完成数
=============================
1 8
2 16
3 20
4 27
5 30
6 33


谢谢帮忙
...全文
341 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuguanlin 2009-06-06
  • 打赏
  • 举报
回复
select t1.empid,t1.val ,SUM(t2.val)
from
ashao as t1
join
ashao as t2
on t1.empid = t2.empid
and t1.val >= t2.val
group by t1.empid,t1.val
order by t1.empid
--小F-- 2009-06-06
  • 打赏
  • 举报
回复
select 编号,完成数=(select sum(完成数) from ta where 编号<=a.编号) from ta a
sdhdy 2009-06-06
  • 打赏
  • 举报
回复
declare @tb table(编号  int, 完成数  int)            

insert @tb select 1, 8
insert @tb select 2 , 8
insert @tb select 3 , 4
insert @tb select 4 , 7
insert @tb select 5 , 3
insert @tb select 6 , 3

declare @count int
set @count=0
update @tb set @count=@count+完成数,完成数=@count
select * from @tb

/*
编号 完成数
----------- -----------
1 8
2 16
3 20
4 27
5 30
6 33

(所影响的行数为 6 行)
*/
JonasFeng 2009-06-06
  • 打赏
  • 举报
回复
SELECT 编号,(select sum(完成数) from LI where 编号 < A.编号) AS 完成数
FROM LI A
hyde100 2009-06-06
  • 打赏
  • 举报
回复
select 
编号,
完成数=(select sum(完成数) from ta b where b.编号<=a.编号)
from ta a
--小F-- 2009-06-06
  • 打赏
  • 举报
回复
Select 编号,sum(完成数) from tb a where not exists(select 1 from tb where 编号<=a.编号)
ks_reny 2009-06-06
  • 打赏
  • 举报
回复

select 编号,完成数=(select sum(完成数) from tb where 编号<=a.编号) from tb a

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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