I have a table like this:
+------+-------+
|ID | value |
+------+-------+
| 1 | 150 |
| 2 | |
| 3 | |
| 4 | |
| 5 | 530 |
| 6 | 950 |
| 7 | 651 |
+-------+------+
I want to copy the last 3 values and at the end my table will look like this:
+------+-------+
|ID | value |
+------+-------+
| 1 | 150 |
| 2 | 530 |
| 3 | 950 |
| 4 | 651 |
| 5 | 530 |
| 6 | 950 |
| 7 | 651 |
+-------+------+
Is it possible?
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.