如何根据以前的记录值更新日期

时间:2021-02-26 17:20:44

标签: postgresql

我正在尝试更新,如果员工有终止日期,则将下一个加入日期更新为终止日期 - 在 postgresql 中为 1 天。不应更新第一个 Join_date。

示例:

<头>
EmpID Dept_No Join_Date termination_date ID
1001 11 2020-01-01 2020-02-01 183
1001 12 2020-02-02 2020-04-01 182
1001 11 2020-04-02 186

下面是我想要得到的输出。

<头>
Dept_No 加入日期 终止日期
11 2020-01-01 2020-02-01
12 2020-02-01 2020-04-01
11 2020-04-01

以下是我目前所拥有的,

UPDATE employee e
SET Join_Date =
((termination_date) -INTERVAL '1 DAY')
WHERE termination_date IS NOT NULL;

但我不确定如何添加以下条件,

  1. 不要更新第一个 Join_date
  2. 使用上一条记录中的终止日期。

有人可以建议如何添加吗?提前致谢。

1 个答案:

答案 0 :(得分:1)

file.truncate 语句中使用 def file_redact(file_name: str): line_to_replace = int(input('Enter the line to replace (starting from 1): ')) f = open(file_name, 'r+') file_data = f.readlines() file_data[line_to_replace - 1] = input('Write a new line:\n') + '\n' f.truncate(0) f.seek(0) f.writelines(file_data) f.close() file_redact("test.txt")

EXISTS

参见demo
结果:

<头>
empid dept_no join_date termination_date id
1001 11 2020-01-01 2020-02-01 183
1001 12 2020-02-01 2020-04-01 182
1001 11 2020-04-01 null 186
相关问题