这和游标有什么关系?
SELECT a1.id,
a2.score
FROM a a1
CROSS APPLY (SELECT SUM(score) score
FROM a
WHERE id <= a1.id
) a2
DECLARE @n int
DECLARE @m int
set @n=1
set @score=0;
select @id=max(id) from A
select id,score from A where id=@n
while(@n<@id+1)
BEGIN
select @m=(select score from A where id=@n) from A
select @score=(select score from A where id=@n+1) from A
set @score=@score+@m
select id,score=@score from A where id=@n+1
set @n=@n+1
END
这是我写的SQL语句,显示的效果是下一行的值是上一行加该行的值得到最终的值,每次用的是原始值,而不是上一行值加下一行值得到的最终值再加下一行
,同时由于是循环执行的所以id和score是打印了四次;这是缺点,但是你可以在这个基础上做你想要的修改,对你对我都是很好的学习吧;
如果回答对您有帮助,请采纳
还有DECLARE @id int
DECLARE @score int
被过滤掉了,你可以自己添加