How To Update The Date Based On Previous Record Value

user2926497

I am trying to update, if an employee has a termination_date, then update the next join_date to termination_date - 1 day in postgresql. The first Join_date should not be updated.

Example:

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

Below is the output that I am trying to get.

Dept_No Join_Date termination_date
11 2020-01-01 2020-02-01
12 2020-02-01 2020-04-01
11 2020-04-01

Below is what I have so far,

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

But I am not sure how to add the below conditions,

  1. Don't update the first Join_date
  2. use the termination_date from the previous record.

Could anyone advice how to add that? Thanks in advance.

forpas

Use EXISTS in the UPDATE statement:

UPDATE employee AS e1
SET Join_Date = e1.Join_Date -INTERVAL '1 DAY'
WHERE EXISTS (
  SELECT 1 
  FROM employee e2 
  WHERE e2.EmpID = e1.EmpID AND e2.termination_date IS NOT NULL
    AND e2.Join_Date < e1.Join_Date 
)

See the demo.
Results:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How To Update The Date Based On Previous Record Value

From Dev

Update column with previous record based on date

From Dev

How to add one to the value of the record based on the previous record

From Dev

UPDATE record based on record from previous day

From Dev

How to update column of spark dataframe based on the values of previous record

From Dev

SQL - Update Record Based on Entity's Previous Record

From Dev

Update value based on the previous updated value mysql

From Dev

Update table based on last date of previous month

From Dev

Update based on previous value SQL SERVER 2005

From Dev

How to delete the record based on last character of previous record in a file

From Dev

How to update a MySQL row based on previous row?

From Dev

SQL: how to update a row based on a previous row

From Dev

JDBC - How to update record where date -1

From Dev

How to update a MySql field based upon value of another field in the same record

From Dev

Update column value in XTS object based on date

From Dev

MySQL Update Value of column Based on DATE

From Dev

How to get previous record date for a group in a select query

From Dev

How to get the previous row value with an ORDERING BY DATE?

From Dev

Updating column based on previous record

From Dev

How to get the record with the smallest date value

From Dev

Updating table 'previous record' based on their 'new record'

From Dev

How to query record based on timestamp and boolean value?

From Dev

How to use SQL previous records calculated value in current record?

From Dev

How to merge create a column in R that only take one value based on update date

From Dev

How to update pandas DataFrame based on the previous row information

From Dev

How to update SQL column based on previous search result of the same table

From Dev

How to update columns data based on previous rows of a result set

From Dev

How to get a previous week number based on current date in c#?

From Dev

How to increment date range by a value from a date record in pandas python?

Related Related

  1. 1

    How To Update The Date Based On Previous Record Value

  2. 2

    Update column with previous record based on date

  3. 3

    How to add one to the value of the record based on the previous record

  4. 4

    UPDATE record based on record from previous day

  5. 5

    How to update column of spark dataframe based on the values of previous record

  6. 6

    SQL - Update Record Based on Entity's Previous Record

  7. 7

    Update value based on the previous updated value mysql

  8. 8

    Update table based on last date of previous month

  9. 9

    Update based on previous value SQL SERVER 2005

  10. 10

    How to delete the record based on last character of previous record in a file

  11. 11

    How to update a MySQL row based on previous row?

  12. 12

    SQL: how to update a row based on a previous row

  13. 13

    JDBC - How to update record where date -1

  14. 14

    How to update a MySql field based upon value of another field in the same record

  15. 15

    Update column value in XTS object based on date

  16. 16

    MySQL Update Value of column Based on DATE

  17. 17

    How to get previous record date for a group in a select query

  18. 18

    How to get the previous row value with an ORDERING BY DATE?

  19. 19

    Updating column based on previous record

  20. 20

    How to get the record with the smallest date value

  21. 21

    Updating table 'previous record' based on their 'new record'

  22. 22

    How to query record based on timestamp and boolean value?

  23. 23

    How to use SQL previous records calculated value in current record?

  24. 24

    How to merge create a column in R that only take one value based on update date

  25. 25

    How to update pandas DataFrame based on the previous row information

  26. 26

    How to update SQL column based on previous search result of the same table

  27. 27

    How to update columns data based on previous rows of a result set

  28. 28

    How to get a previous week number based on current date in c#?

  29. 29

    How to increment date range by a value from a date record in pandas python?

HotTag

Archive