Skip to content
Advertisement

UPDATE rows with values from the same table

I have a table like this:

I want to copy the last 3 values and at the end my table will look like this:

Is it possible?

Advertisement

Answer

Use a self-join:

If there are gaps in the ID space, generate gapless IDs with the window function row_number(). I do that in a CTE, because I am going to reuse the table twice for a self-join:

You need PostgreSQL 9.1 or later for data-modifying CTEs.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement