如何向其连续添加开始行和下一行的值 [英] How to continously add values of starting row and next row to it
本文介绍了如何向其连续添加开始行和下一行的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只想创建一个sql查询,其结果类似于图像上的内容,类似于SQL中的斐波那契数列.
i just want to create an sql query and the result is something like on the image., something like Fibonacci sequence in SQL.
例如.
第1列:10,那么Result列的值为Result:10,因为那是第一行.,然后假设column1第二行的值为50,那么Result第二行的值为60 ..(结果:60).依此类推.
Column 1: 10 , then the value of Result column is Result: 10 , since that is the first row. , then assuming that the value of column1 2nd row is 50, then the value of Result 2nd row will be 60.. (Result: 60).. and so on.
示例是下面的图像.
我如何连续地做到这一点?任何帮助,将不胜感激.谢谢
How can i do that continuously ? any help would be appreciated. Thanks
推荐答案
如果您使用的是 MSSQL2012
或更高版本,则可以使用 OVER
子句.
If you are using MSSQL2012
or higher you can use OVER
clause.
SELECT t2.id, t2.value, SUM(t2.value) OVER (ORDER BY t2.id) as [Result]
FROM Test01 t2
ORDER BY t2.id;
这篇关于如何向其连续添加开始行和下一行的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文