SQL select 按行增加运行总计列的数量
假设我有一个包含列(DayId、RunningTotal)的表:
DayId RunningTotal
---------------------
1 25
3 50
6 100
9 200
10 250
如何选择 DayId 以及 RunningTotal 较前一天增加的金额?即我如何选择:
DayId DayTotal
---------------------
1 25
3 25
6 50
9 100
10 50
我知道的唯一当前方法是使用我试图分解的 while 循环。另外,DayId 没有规则的规则,只是它是一些递增的整数值,但它的增长量不规则,如示例表所示。
编辑:使用 MS SQL Server 2005
Suppose I have a table with columns (DayId, RunningTotal):
DayId RunningTotal
---------------------
1 25
3 50
6 100
9 200
10 250
How can I select the DayId and the amount the RunningTotal has increased from the previous day? i.e. how can I select:
DayId DayTotal
---------------------
1 25
3 25
6 50
9 100
10 50
The only current method I know is with a while loop I am trying to factor out. Also, the DayId has no regular rules, just that it is some increasing integer value, but it increases by an irregular amount as shown in the example table.
EDIT: using MS SQL Server 2005
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(我真的希望他们能够在 SQL Server 中实现对
lead
和lag
函数的支持:|)(I really wish they'd implemented support for the
lead
andlag
functions in SQL Server :|)可能有比这更简洁的方法,但请尝试:
There is probably a more succinct way than this, but try: