Sql如何将前一行的值添加到下一行 [英] Sql how to add value from previous row into the next

查看:843
本文介绍了Sql如何将前一行的值添加到下一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am trying to add the value of the previous row to the current row into the column cumulative







Select 
    Ddate as Date, etype, Reference, linkacc as ContraAcc,
    Description,
    sum(case when amount > 0 then amount else 0 end) as Debits,
    sum(case when amount < 0 then amount else 0 end) as Credits,
    sum(amount) as Cumulative
from
    dbo.vw_LT
where
    accnumber ='8400000' 
    and [DDate] between '2016-04-01 00:00:00' and '2016-04-30 00:00:00'
    and [DataSource] = 'PAS11CEDCRE17'
group by 
    Ddate, etype, Reference, linkacc, Description, Amount
Output:

Date        Reference   ContraAcc   Description Debits  Credits Cumulative 
--------------------------------------------------------------------------
2016-04-01     CC007    8000000     D/CC007      0      -39.19    -39.19
2016-04-01     CC007    8000000     D/CC007     1117.09     0     1117.09
2016-04-01     CC009    8000000     CC009       2600        0       2600





在累积列第二行必须是-39.19 + 1117.09等等



我尝试过:



i试图使用声明总和(金额)作为累积,但它正在提供相同的值。



in the cumulative column row two must be -39.19 + 1117.09 and so on

What I have tried:

i have tried to use the statement sum(amount) as Cumulative but it is brining the same value.

推荐答案

SQL SERVER - 如何在SELECT语句中访问Previous Row和Next Row值? - 第4部分 - 使用Pinal Dave访问SQL权限 [ ^ ]


这篇关于Sql如何将前一行的值添加到下一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆