如何从以下场景更新表格
表:DIM_SCLASS
SCLASS_CODE | GL_CLASS_CODE | RPT_GRP | UW_YEAR_START| UW_YEAR_END
PA01 | 06 | P | 1900 | 2017
PA01 | 06 | P | 1900 | 2020
PA01 | 05 | V | 2021 | 2500
我的问题: 1.如何从ROW 1 UW_YEAR_END更新ROW 2 UW_YEAR_START值?
请在方案中找到上面的图片。enter image description here
答案 0 :(得分:0)
这将更新所有行。添加WHERE
子句以限制更新哪些行(如果需要)。
UPDATE d
SET d.UW_YEAR_START = LAG(d.UW_YEAR_END,1) OVER (ORDER BY d.UW_YEAR_END asc)
FROM DIM_SCLASS d
答案 1 :(得分:0)
您应该可以通过加入来执行此操作。尝试这样的事情:
update DIM_SCLASS
set r1.UW_YEAR_START = r2.UW_YEAR_START
FROM DIM_SCLASS r1
join DIM_SCLASS r2
on r1.SCLASS_CODE = r1.SCLASS_CODE -- YOU CAN USE 1 = 1 OR SOME OTHER CONDITION IF NECESSARY
where r1.ROW_NUMBER() = 1
AND r2.ROW_NUMBER() = 2;