By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE
INSERT 0 9
| id | document_id | value | precedence_level | updated_at |
|---|---|---|---|---|
| 1 | 1 | Z | DEFAULT | 2025-01-01 |
| 2 | 1 | Y | DEFAULT | 2025-01-02 |
| 3 | 1 | X | DEFAULT | 2025-01-03 |
| 4 | 1 | E | IMPORTANT | 2025-01-04 |
| 5 | 1 | F | DEFAULT | 2025-01-05 |
| 6 | 1 | D | IMPORTANT | 2025-01-06 |
| 7 | 1 | C | RESET | 2025-01-07 |
| 8 | 1 | B | DEFAULT | 2025-01-08 |
| 9 | 1 | A | IMPORTANT | 2025-01-09 |
SELECT 9
| rnk_final | rnk | id | document_id | value | precedence_level | updated_at | prev_reset_count | prev_value |
|---|---|---|---|---|---|---|---|---|
| 1 | 1 | 9 | 1 | A | IMPORTANT | 2025-01-09 | 1 | B |
| 2 | 2 | 8 | 1 | B | DEFAULT | 2025-01-08 | 1 | C |
| 3 | 3 | 7 | 1 | C | RESET | 2025-01-07 | 1 | D |
| 4 | 4 | 6 | 1 | D | IMPORTANT | 2025-01-06 | 0 | E |
| 5 | 5 | 4 | 1 | E | IMPORTANT | 2025-01-04 | 0 | F |
| 6 | 6 | 5 | 1 | F | DEFAULT | 2025-01-05 | 0 | X |
| 7 | 7 | 3 | 1 | X | DEFAULT | 2025-01-03 | 0 | Y |
| 8 | 8 | 2 | 1 | Y | DEFAULT | 2025-01-02 | 0 | Z |
| 9 | 9 | 1 | 1 | Z | DEFAULT | 2025-01-01 | 0 | null |
SELECT 9
| id | document_id | value | precedence_level | updated_at | prev_reset_count |
|---|---|---|---|---|---|
| 1 | 1 | Z | DEFAULT | 2025-01-01 | 0 |
| 2 | 1 | Y | DEFAULT | 2025-01-02 | 0 |
| 3 | 1 | X | DEFAULT | 2025-01-03 | 0 |
| 4 | 1 | E | IMPORTANT | 2025-01-04 | 0 |
| 5 | 1 | F | DEFAULT | 2025-01-05 | 0 |
| 6 | 1 | D | IMPORTANT | 2025-01-06 | 0 |
| 7 | 1 | C | RESET | 2025-01-07 | 1 |
| 8 | 1 | B | DEFAULT | 2025-01-08 | 1 |
| 9 | 1 | A | IMPORTANT | 2025-01-09 | 1 |
SELECT 9
| rnk | id | document_id | value | precedence_level | updated_at | prev_reset_count |
|---|---|---|---|---|---|---|
| 1 | 9 | 1 | A | IMPORTANT | 2025-01-09 | 1 |
| 2 | 8 | 1 | B | DEFAULT | 2025-01-08 | 1 |
| 3 | 7 | 1 | C | RESET | 2025-01-07 | 1 |
| 4 | 6 | 1 | D | IMPORTANT | 2025-01-06 | 0 |
| 5 | 4 | 1 | E | IMPORTANT | 2025-01-04 | 0 |
| 6 | 5 | 1 | F | DEFAULT | 2025-01-05 | 0 |
| 7 | 3 | 1 | X | DEFAULT | 2025-01-03 | 0 |
| 8 | 2 | 1 | Y | DEFAULT | 2025-01-02 | 0 |
| 9 | 1 | 1 | Z | DEFAULT | 2025-01-01 | 0 |
SELECT 9
| int4 | int4 | int4 | int4 |
|---|---|---|---|
| 1 | 0 | 1 | 0 |
SELECT 1
| rnk | id | document_id | value | precedence_level | updated_at | prev_reset_count | prev_value |
|---|---|---|---|---|---|---|---|
| 1 | 9 | 1 | A | IMPORTANT | 2025-01-09 | 1 | B |
| 2 | 8 | 1 | B | DEFAULT | 2025-01-08 | 1 | C |
| 3 | 7 | 1 | C | RESET | 2025-01-07 | 1 | D |
| 4 | 6 | 1 | D | IMPORTANT | 2025-01-06 | 0 | E |
| 5 | 4 | 1 | E | IMPORTANT | 2025-01-04 | 0 | F |
| 6 | 5 | 1 | F | DEFAULT | 2025-01-05 | 0 | X |
| 7 | 3 | 1 | X | DEFAULT | 2025-01-03 | 0 | Y |
| 8 | 2 | 1 | Y | DEFAULT | 2025-01-02 | 0 | Z |
| 9 | 1 | 1 | Z | DEFAULT | 2025-01-01 | 0 | null |
SELECT 9