运行总和减去不同的条件 [英] Runing total sum minus different condition

查看:57
本文介绍了运行总和减去不同的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我看了一些sql server运行总示例但我不能管理这样的事情:

i有表我有id,名称,操作类型,日期,值

所以我想计算每个记录的balanace,必须计算余额,如此起始余额必须为0然后如果操作类型为'IN',那么如果操作然后减去则会有加号,它会像下一个一样recored必须看到previus记录余额,然后+ value或-value(取决于操作类型),这个operetion必须按日期排序(不是Id),例如table就像这样:

 ID名称Op_Type日期值
1 框输出2017-05-13 15
2 表在2017-04-31中 65
3 box2在2017-05-31中 65





我有什么ied:



计算后必须像这样
ID名称Op_Type日期值余额
2 table在2017-04-31 65 65
1 box out out 2017-05-13 15 50
3 box2在2017-05-31 65 115

解决方案

我想你可以在这里找到答案:运行总计Sum()问题 [ ^

Hello guys i looked some sql server running total examples but i can't manage thing like this :
i have table where i have id,name,type of operation,date,value
so i want to calculate balanace for each recored , balance must be calculted like this starting balance must be 0 and then if operation type is 'IN' there will be plus if operation out then minus and it goes like every next recored must see previus record balance and then +value or -value (depends on operation Type) and this operetion must by ordered by date (not Id) for example table is like this:

ID Name  Op_Type Date       Value
1  box   Out     2017-05-13 15
2  table In      2017-04-31 65
3  box2  In      2017-05-31 65



What I have tried:

after calculateing it must be like this 
ID Name  Op_Type Date       Value Balance
2  table In      2017-04-31 65     65
1  box   Out     2017-05-13 15     50
3  box2  In      2017-05-31 65     115

解决方案

I think you can find the answer here: Running Total Sum() Problem[^]


这篇关于运行总和减去不同的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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