I have two tables: TableA
Id Status User
1 15 111
2 15 111
3 15 111
And TableB which contains history of status changes from previous table:
Id IdA Status Date
1 1 10 2023-01-18
2 1 30 2022-12-18
3 3 30 2022-01-17
4 3 10 2022-01-16
What I need to do is to update status field values for every row with User 111 in TableA with values from TableB, I need to find the latest entity change in TableB and write its status to the corresponding entity in TableA.
So the final result for the TableA should be:
Id Status User
1 10 111
2 15 111
3 30 111
2
Answers
Rank table b based on IdA ordered by date ascending then choose row number 1 to update table a.
Row_number()over(order by …)
tableb is the second table and the tablen is the 1 table
this clause is to get the last update
See the docs for DISTINCT ON clause.
See the demo with all the details.