如何将上一行值添加到下一行?

如何将上一行值添加到下一行?

问题描述:

我有桌子像



id credit



1 1000



2 2000



3 3000



i需要一个结果集以下



id贷方余额



1 1000 1000



2 2000 3000



3 3000 6000





我试过这个查询



选择t1.id,t1.credit),t1.credit + t2.credit,来自table1 t1内部JOIN table1 t2 ON t1 .ID = t2.id - 1

I have table like

id credit

1 1000

2 2000

3 3000

i need a result set like the below

id credit balance

1 1000 1000

2 2000 3000

3 3000 6000


I tried with this query

Select t1.id ,t1.credit),t1.credit+t2.credit, from table1 t1 Inner JOIN table1 t2 ON t1.ID = t2.id - 1

您想要的是运行总计。它不是那么简单,但可以实现。请看这里: http://www.sqlteam.com/article/calculating-running-totals [ ^ ]
What you want is called running total. It is not that simple, but can be achieved. Look here: http://www.sqlteam.com/article/calculating-running-totals[^]


在SQL Server中计算简单运行总计 [ ^ ] br />


http://beyondrelational.com/modules/1/justlearned/0/tips/9272/sql-server-2012-calculating-running-total-is-easy-now.aspx [ ^ ]



希望这个链接能给你一个想法......
Calculating simple running totals in SQL Server[^]

http://beyondrelational.com/modules/1/justlearned/0/tips/9272/sql-server-2012-calculating-running-total-is-easy-now.aspx[^]

Hope this links will give you idea....