SQL 从运行总数中删除

SQL remove from running total(SQL 从运行总数中删除)
本文介绍了SQL 从运行总数中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个问题,我不知道如何解决..这是代码和想要的结果

I have a problem that I don't know how to fix .. here is the code and wanting result

if object_id('tempdb..#A') IS NOT NULL drop table #A
create table #A (ID int, Value decimal(6,2), value2 decimal(6,2), Result decimal(6,2))

insert into #A (ID, Value, value2, Result)
values
(1, 10, 25, null),
(1, 10, 25, null),
(1, 10, 25, null),
(2, 10, 5, null),
(2, 10, 5, null),

select * from #A

所以,我想从value2"中取出价值,如果有剩余,只需将其更新为 0,对于 下一行,我将采取那些剩余"并用它们带走,与下一个价值

So, I would like to take Value away from "value2", if there are left overs, just update it to 0, for next row i would take those "left overs" and use them to take away from, with next Value

我想得到这样的结果...

I would like to get results like this...

ID  Value     value2    Result
 1    10        25        0
 ----------------------------
 1    10        25        0
 ----------------------------
 1    10        25        5
 ----------------------------
 2    10        5         5
 ----------------------------
 2    10        5         10

如您所见,ID 为 1 ... 应该是:

So as you can see with ID 1 ... it would be:

10 - 25 = 0
10 - 15 = 0
10 - 5  = 5

我希望你明白我在这里想要做什么......如果我能解释更多,请告诉我......

I hope you understand what I am trying to do here ... let me know if I can explain more ...

推荐答案

在 Gordon 的帮助下并使用了他的部分想法......我做了一些事情,目前看来可行,但还需要更多测试

With help of Gordon and using some part of his idea ... i did something, that at this moment seems to work, but will need a lot of more testing

if object_id('tempdb..#testDataWithRunningTotal') IS NOT NULL drop table #testDataWithRunningTotal
select id, value, value2, cast(null as float) as Result 
   into #testDataWithRunningTotal
from #A order by id;

declare @runningTotal float = 0, @previousParentId int = null;

update #testDataWithRunningTotal
set
   @runningTotal = Result = case when @previousParentId <> id 
                                then value2 - value                                     
                            else 
                                case when ISNULL(@runningTotal,0) < 0 then value * (-1)
                                     when value2 - value < 0 and ISNULL(@runningTotal,0) = 0 then value2 
                                     when value2 - value > 0 and ISNULL(@runningTotal,0) = 0 then value2 - value                                
                                else
                                     case when @runningTotal - value < 0 and ISNULL(@runningTotal,0) = 0 then value 
                                          else @runningTotal - value
                                     end
                                end
                            end,
   @previousParentId = id
from #testDataWithRunningTotal 

update tst
set Result = case when Result > 0 
            then 0 
            else Result * -1 
         end
from #testDataWithRunningTotal tst

select * from #testDataWithRunningTotal

所以,我保持@runningTotal 运行更新,并允许它低于 0 ......一旦它小于 0,这意味着值的总和大于 Value2 的总和的时刻......所以我将记录保留在那里,并在此计算结束时进行更新.

So, I am keeping @runningTotal running with update, and allowing it to go under 0 ... once it goes less then 0 it means that is moment where SUM of value is greater then SUM of Value2 ... so i keep the record there, and at end of this calculation i do update.

这篇关于SQL 从运行总数中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
SQL query to group by day(按天分组的 SQL 查询)
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)