I am using PostgreSQL 9.3
I have a table named cat with the three following columns of interest:
ID, SOURCE, TIME
ID and TIME values are unique (i.e. no duplicates) but several rows have the same SOURCE value
I would like to update each value of the SOURCE column, setting it to the ID value of the first input row in each group of rows having the same SOURCE value and ordered in TIME ascending.
In a SELECT statement, I would use:
SELECT
first_value(ID) OVER (PARTITION BY SOURCE ORDER BY TIME ASC) AS SOURCE
FROM cat;
So I tried this for the UPDATE statement:
UPDATE cat
SET SOURCE = first_value(ID) OVER (PARTITION BY SOURCE ORDER BY TIME ASC);
Which returns the following error:
ERROR: window functions are not allowed in UPDATE
Could someone help me to find a fast way of doing this given that cat has ~800 000 rows and 322 columns?