sql - SQL 中基于先前值的动态更新
问题描述
我正在尝试探索动态更新。
我的实际源表是:
更新后源表的预期结果:
我试过的查询:
WITH t AS
(
SELECT key,
Begin_POS,
Length,
(Begin_POS+ Length) as res
from tab
)
SELECT src_column_id,
Length,res,
COALESCE(Length + lag(res) OVER (ORDER BY src_column_id),1) AS PRE_VS
from t
你能帮助我的方法应该是什么吗?
解决方案
我认为这是一个窗口总和:
select
t.*,
1 + coalesce(
sum(length) over(
order by key
rows between unbounded preceding and 1 preceding
),
0
) new_begin_pos
from mytable t
推荐阅读
- django - 如何创建 Firefox 健康检查
- django - django bootstrap 表单中的提交按钮不起作用
- swiftui - 如何更改 TextField 中的字段大小?
- asp.net - 是否可以使用 ASP.net 调用同步 http post 请求?
- kotlin - 从具有 runtimeOnly 依赖项的模块注册 Koin 组件
- django - 'choices' 必须是包含(实际值,人类可读名称)元组的可迭代 django 3.1
- javascript - 通过不同的按钮单击在数组上添加和删除元素
- swift - 表格单元格未在 Swift 上适当调整高度
- android - 无法从协程 withContext(Dispatchers.Main.imidiate){} 更新类变量的值
- assembly - 使用 makefile 在 Raspberry Pi 4 上组装