I'm trying to update a column cohort_number in a table with sequential values from 1 to x (in this case 42) using a seq as below.
CREATE SEQUENCE seq start with 1 increment by 1 no maxvalue no cycle
UPDATE t1
SET cohort_number = next value for seq, instance_number = x.instanceNumber
FROM
(
SELECT id,
1 instanceNumber
FROM t1
) x
DROP SEQUENCE seq
However on update rather than the first row cohort_number = 1, second row = 2 and so on, the first row cohort_column = 42, second row cohort_column = 2. I.e it appears that the update is being processed from the last entry in the select result rather than the first. Is there anyway around this?
I've also tried with ROW_NUMBER as below, but every value for the cohort_column is set to 1, which I believe is due to id having no duplicate values (but I'm guessing here)
UPDATE t1
SET cohort_number = x.cohort_number, instance_number = x.instance_number
FROM
(
SELECT id,
ROW_NUMBER() over (order by id) as cohort_number,
1 instance_number
FROM t1
) x