I have Postgres 9.5 with a table movimientos that has the following data:
| id | concepto | movimiento | numero | orden |
| 1 | AJUSTE 1 | 2542 | 0 | 2 |
| 2 | APERTURA | 12541 | 0 | 1 |
| 3 | AJUSTE 2 | 2642 | 0 | 2 |
| 4 | CIERRE | 22642 | 0 | 3 |
And I need to number the records based on the orden field and keep these numbers in the numero field, because I need this data to sort and search by numero in reports. Example:
| id | concepto | movimiento | numero | orden |
| 2 | APERTURA | 12541 | 1 | 1 |
| 1 | AJUSTE 1 | 2542 | 2 | 2 |
| 3 | AJUSTE 2 | 2642 | 3 | 2 |
| 4 | CIERRE | 22642 | 4 | 3 |
I tried to do it using a function with a FOR but is very slow with a million rows.
How to do this using a simple UPDATE?