I have a list of records in the below format (example) produced by joining multiple tables from the database and where conditions:
Col1 | Col2 | Col3 |
---|---|---|
100 | 200 | 1 |
100 | 201 | 1 |
100 | 202 | 1 |
100 | 203 | 1 |
101 | 204 | 1 |
101 | 205 | 1 |
102 | 206 | 1 |
102 | 207 | 1 |
What I would like is to update the values in Col3 in the above example based on the values in Col1 and Col2.
The idea is to loop through Col1 first and then another loop in Col2 and update the values in Col3 starting from 1 and increment by 1 per each iteration over Col2 record. This should be repeated again for the next iteration on Col1.
A sample output expected from the above method is:
Col1 | Col2 | Col3 |
---|---|---|
100 | 200 | 1 |
100 | 201 | 2 |
100 | 202 | 3 |
100 | 203 | 4 |
101 | 204 | 1 |
101 | 205 | 2 |
102 | 206 | 1 |
102 | 207 | 2 |
The database used is postgres and I am quite new to the functions like cursor in postgres. If anyone have any insight on this to have an efficient way to workout this problem will be great.
Appreciate your help thanks.
Thanks