SQL Server-反向累积总和

问题描述

我有一个需要累计的累计金额。如何在SQL Server中做到这一点?

示例:

+---+-------------------+-----+
| id|               date|value|
+---+-------------------+-----+
| J1|2016-10-01 11:45:30|  100|
| J1|2016-10-02 11:30:30|  200|
| J1|2016-10-05 16:20:00|  400|
| J9|2016-10-06 08:35:00|  800|
| J9|2016-10-07 01:20:00|  900|
+---+-------------------+-----+

所需的数据框:

+---+-------------------+-----+---------+
| id|               date|value|non_cum_value|
+---+-------------------+-----+---------+
| J1|2016-10-01 11:45:30|  100|        0|
| J1|2016-10-02 11:30:30|  200|      100|
| J1|2016-10-05 16:20:00|  400|      200|
| J9|2016-10-06 08:35:00|  800|      400|
| J9|2016-10-07 01:20:00|  900|      100|
+---+-------------------+-----+---------+

我的代码:

select t1.id,t1.value,DIFFERENCE(t1.value) as 'cum_sum'
from @t t1
inner join @t t2 on t1.id >= t2.id
group by t1.id,t1.value
order by t1.id

解决方法

嗯。 。 。您似乎想从“上一个”行中减去该值。那应该是:

select t.*,(t.value - lag(t.val) over (order by date)) as diff
from @t;

我不确定标题中为什么有“累计金额”。那并不能帮助我理解您要做什么。

,

另一种可能性是:

max-width: 100%

假设测试数据为:

let homme = document.getElementById("homme");
let femme = document.getElementById("femme");
homme.addEventListener("click",function(){
    homme.classList.add("active");
    femme.classList.remove("active");
});
femme.addEventListener("click",function(){
    femme.classList.add("active");
    homme.classList.remove("active");
});

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...