首页 > 解决方案 > 如何使用 SQL 使用 DB2 中现有的 Holiday 表将字段更新到下一个工作日

问题描述

(DB2)以今天为例(2020-04-30),2020-05-01是我国的国定假日,2020-05-02、2020-05-03是周末,所以今天的下一个工作日是2020-05-04。

所有这三天(2020-05-01、2020-05-02、2020-05-03)都记录在“假期”表中。我只想将另一个表的值更新到下一个工作日。有什么办法吗?

标签: sqldb2

解决方案


你可以做:

with 
d as (select h, h + 1 day as next_day from holiday where h >= '2020-04-30')
select min(next_day) from d where next_day not in (select h from d);

请参阅DB Fiddle上的运行示例。

编辑更新

UPDATE在另一个表上执行,您可以执行以下操作:

update a
set d = (
  select min(next_day) from (
    select h, h + 1 day as next_day from holiday where h >= '2020-04-30'
  ) x where next_day not in (select h from holiday where h >= '2020-04-30')
)

请参阅DB Fiddle上的运行示例。


推荐阅读