sql中怎么循环处理当前行数据和上一行数据相加减

数据库   2024年05月10日 2:40  

本篇内容主要讲解“sql中怎么循环处理当前行数据和上一行数据相加减”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“sql中怎么循环处理当前行数据和上一行数据相加减”吧!

 以下事例,使用游标循环表#temptable中数据,然后让当前行和上一行中的argument1 相加 存放到当前行的 argument2 中,比较简单。

--droptable#temptablecreatetable#temptable(argument1int,argument2int,argument3datetime)declare@rowcountint,@argument1int,@argument2nvarchar(50),@argument3datetimeset@rowcount=1set@argument1=1set@argument2=0set@argument3=GETDATE()while(@rowcount<100)begininsertinto#temptable(argument1,argument2,argument3)values(@argument1,@argument2,@argument3)set@argument1=@argument1+datepart(day,@argument3)set@argument3=@argument3-1set@rowcount=@rowcount+1end--select*from#temptabledeclare@lastargument2intset@lastargument2=0set@argument2=0declare_cursorcursorfor(selectargument1from#temptable)open_cursor;fetchnextfrom_cursorinto@argument2while@@fetch_status=0beginupdate#temptablesetargument2=@argument2+@lastargument2wherecurrentof_cursorset@lastargument2=@argument2fetchnextfrom_cursorinto@argument2endclose_cursordeallocate_cursor--select*from#temptable

到此,相信大家对“sql中怎么循环处理当前行数据和上一行数据相加减”有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

域名注册
购买VPS主机

您或许对下面这些文章有兴趣:                    本月吐槽辛苦排行榜

看贴要回贴有N种理由!看帖不回贴的后果你懂得的!


评论内容 (*必填):
(Ctrl + Enter提交)   

部落快速搜索栏

各类专题梳理

网站导航栏

X
返回顶部