db2 - db2 sql update 计算总计列单次更新
问题描述
试图计算年度列存储桶的移动总数,每月存储桶也在滚动,所以我不确定是否可能在单次更新中,我希望 prev12 列等于移动后每月更新的 1-12
update table_a as a1
set (a1.col1, a1.col2, a1.col3,a1.col4,a1.col5,a1.col6,
a1.col7, a1.col8,a1.col9, a1.col10,a1.col11,a1.col12,
a1.col_prev12) =
(select
a2.col, a2.col1, a2.col2, a2.col3,
a2.col4, a2.col5, a2.col6,
a2.col7, a2.col8, a2.col9,
a2.col10, a2.col11,
a2.col1 + a2.col2 + a2.col3 + a2.col4 +
a2.col5 + a2.col6 + a2.col7 + a2.col8 +
a2.col9 + a2.col10 + a2.col11 + a2.col12
from table_a as a2
where a1.store = a2.store and a1.line = a2.line and a1.item = a2.item
)
where a1.last_roll_date = current date
解决方案
不太确定你想要什么,但是你可以在 UPDATE 中更新多个列
此外,您可能会发现这种形式的 UPDATE 更容易
UPDATE (
SELECT *
, LAG(COL1) OVER(PARTITION BY store, line ORDER BY last_roll_date) as PREV_COL1
, LAG(COL1) OVER(PARTITION BY store, line ORDER BY last_roll_date) as PREV_COL2
FROM
table_a
)
SET COL1 = PREV_COL1
, COL2 = PREV_COL2
WHERE
last_roll_date = current date
或者,如果不是,您可能只是 where a1.last_roll_date = a2.last_role_date - 1 month
原始子选择中的一个
推荐阅读
- java - 在 Visual Code 中生成 Javadoc 文件的扩展
- swiftui - SwiftUI,将标题设置为 NavigationView 内 TabView 的子视图不起作用
- javascript - odoo 12 中的 JavaScript
- python - Python - 在每月的一周进行聚合并进行比较
- asp.net-mvc - 无法从 HttpResponseMessage 中的发布请求中获得响应
- javascript - chrome 浏览器的滚动条“重叠”UI 而不是推送
- hibernate - (自定义存储库 + 休眠)或使用 JPA 存储库之间的最佳选择是什么?
- tensorflow - 有什么方法可以将 tensorflow lite (.tflite) 文件转换回 keras 文件 (.h5)?
- python - 安装 python 包 Flair 时出错,关于不在 PyPI 中托管的依赖包
- c# - 将类类型添加到数据库