This is a duplicate of this question for Postgres Update records that satisfies a condition with incrementing number but I need a way that will work with SQLite3.
Snipping from the original question:
SNIP
I have a table in postgres like this:
Id Name local_site_id local_id
1 A 2
2 B 2
3 C 1
4 D 2
5 E 1
How do I update the table into this using SQL query:
Id Name local_site_id local_id
1 A 2 1
2 B 2 2
3 C 1
4 D 2 3
5 E 1
Right now, the local_id field is empty for all the records. I want to update the local_id values with an incrementing number starting from 1 only for rows that have local_site_id=2 Is it possible using SQL?
END-SNIP
I tried this command from the answer there but it doesn’t work for SQLite3
update T set local_id=s.rn
from (select id,row_number() over(order by id) as rn from T where local_site_id=2) s
where T.id=s.id;
How can I achieve this in SQLite3?
This should do it:
which returns: