SQL Server 逐行操作
我正在尝试根据“值”列中的前一行记录在“增量”列中执行一些操作,
例如
row_num| Period | Measure | Decay
1 | Jan 08 | 10 |
2 | Feb 08 | 18 |
3 | Mar 08 | 7 |
4 | Apr 08 | 67 |
我想根据公式更新“衰减”列,
row_num| Period | Measure| Decay
1 | Jan 08 | 10 | = 10 -> first value in 'Measures'
2 | Feb 08 | 18 | = 10*0.5+18 = 23 -> previous decay record *0.5 + current measure
3 | Mar 08 | 7 | = 23*0.5+7 = 18.5
4 | Apr 08 | 67 | = 18.5*0.5+67 = 76.25
光标是否适用于此处?语法会是什么样子? 谢谢
I am trying to do some operation in column 'Increment' based on previous row record in column 'Value'
e.g.
row_num| Period | Measure | Decay
1 | Jan 08 | 10 |
2 | Feb 08 | 18 |
3 | Mar 08 | 7 |
4 | Apr 08 | 67 |
i would like to update column 'Decay' based on a formula
row_num| Period | Measure| Decay
1 | Jan 08 | 10 | = 10 -> first value in 'Measures'
2 | Feb 08 | 18 | = 10*0.5+18 = 23 -> previous decay record *0.5 + current measure
3 | Mar 08 | 7 | = 23*0.5+7 = 18.5
4 | Apr 08 | 67 | = 18.5*0.5+67 = 76.25
would cursor be applicable here? how would the syntax be like?
thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是使用递归 CTE 的一个运行示例(另请注意,您的示例不正确):
Here's a running example using a recursive CTE (also note that the arithmetic in your example is incorrect):