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

发布时间:2021-09-13 21:45:28 作者:chen
来源:亿速云 阅读:757

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

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

--drop table #temptablecreate table #temptable(  argument1 int,  argument2 int,  argument3 datetime)declare @rowcount int,@argument1 int,@argument2 nvarchar(50),@argument3 datetimeset @rowcount=1set @argument1=1set @argument2=0set @argument3=GETDATE()while(@rowcount<100)begin  insert into #temptable(argument1,argument2,argument3)        values(@argument1,@argument2,@argument3)    set @argument1=@argument1 + datepart(day,@argument3)  set @argument3=@argument3-1    set @rowcount = @rowcount + 1end--select * from #temptabledeclare @lastargument2 intset @lastargument2=0set @argument2=0declare _cursor cursor for(select argument1 from #temptable)open _cursor;fetch next from _cursor into @argument2 while @@fetch_status = 0begin        update #temptable  set argument2=@argument2+@lastargument2  where current of _cursor    set @lastargument2=@argument2    fetch next from _cursor into @argument2 endclose _cursordeallocate _cursor--select * from #temptable

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

推荐阅读:
  1. layui-table获得当前行的上/下一行数据的例子
  2. python中如何将循环内容在一行输出

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

sql

上一篇:SQL语句中含有乘号报错的解决方法

下一篇:SQL语句中GROUP BY的用法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》