SQL 字段 = 其他字段减去另一行
表有 2 列:[nr] 和 [diff]
diff 为空(到目前为止 - 需要填充)
nr 有数字:
1
2
45
677
43523452
在 diff 列上,我需要添加对之间的差异,
1 | 0
2 | 1
45 | 43
677 | 632
43523452 | 43522775
所以基本上类似于:
update tbl set diff = @nr - @nrold where nr = @nr
但我不想要接下来使用 fetch,因为它不酷,而且速度很慢(100.000)条记录,
我怎样才能通过一次更新来做到这一点?
Table has 2 cols: [nr] and [diff]
diff is empty (so far - need to fill)
nr has numbers:
1
2
45
677
43523452
on the diff column i need to add the differences between pairs
1 | 0
2 | 1
45 | 43
677 | 632
43523452 | 43522775
so basically something like :
update tbl set diff = @nr - @nrold where nr = @nr
but i don't want to use fetch next, because it's not cool, and it's slow (100.000) records
how can I do that with one update?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看看这样的东西(完整的例子)
Have a look at something like this (full example)
试试这个 -
假设表中只有 nr old 的一个旧行。否则子查询将返回多个值
try this -
This is assuming you only have one older row for nr old in the table. else the subquery will return more than one value