Can't find a way to implement a specific row_number() function in Postgres. See the table below:
I want from row_number to start counter from one for each difference like for items a and c. Third column is what I want.
my code:
select item, flag, row_number() over (partition by item, flag order by item) as rownum from t
item | flag | day | row_number_current | row_number_required |
---|---|---|---|---|
a | 0 | 1 | 1 | 1 |
a | 0 | 2 | 2 | 2 |
a | 1 | 3 | 1 | 1 |
a | 1 | 4 | 2 | 2 |
a | 1 | 5 | 3 | 3 |
a | 0 | 6 | 3 | 1 |
a | 1 | 7 | 4 | 1 |
a | 1 | 8 | 5 | 2 |
b | 0 | 1 | 1 | 1 |
b | 1 | 2 | 1 | 1 |
b | 1 | 3 | 2 | 2 |
b | 1 | 4 | 3 | 3 |
b | 1 | 5 | 4 | 4 |
c | 1 | 1 | 1 | 1 |
c | 1 | 2 | 2 | 2 |
c | 0 | 3 | 1 | 1 |
c | 0 | 4 | 2 | 2 |
c | 1 | 5 | 3 | 1 |
c | 1 | 6 | 4 | 2 |
c | 1 | 7 | 5 | 3 |