我有一个如下表,其中包含一些初始值:
日期 | 值 | 常量 |
---|---|---|
2020-02-01 | 10 | 0.25 |
在MySQL 8+上,我们可以使用SUM
:处理此问题
SELECT
date,
SUM(value) OVER (ORDER BY date) +
SUM(constant) OVER (ORDER BY date
ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) AS new_value,
constant
FROM yourTable
ORDER BY date;
您可以按如下方式使用窗口函数sum
:
select date,
sum(coalesce(value,0) + constant) over (order by date) as constant
from t;