I am trying to do something similar to this but with the extra complication that I need to group by a common field
I have two tables, one for competition_users
(for those competing in competitions) and another competition_times
(to store their fastest times), but there are many different competitions in the same table.
I have created a new column rank
in the competition_times
table and now need to run an update for existing users to show their rank in their competition, I am trying to modify the solution given in the other SO answer without success
The tables are as follows...
competition_users:
competition_user_id
, competition_id
competition_times:
competition_time_id
, competition_user_id
, time_in_seconds
, rank
I am not sure if its possible to use GROUP BY
? or if there is another way, so far I am trying something like this...
UPDATE competition_times
SET rank = r.rnk
FROM (
SELECT competition_time_id
, dense_rank() OVER (ORDER BY time_in_seconds ASC) AS rnk
FROM competition_times, competition_users
WHERE competition_times.competition_user_id = competition_users.competition_user_id
GROUP BY competition_users.competition_id
) r
WHERE competition_times.competition_time_id = r.competition_time_id
Using postgreSQL 9
Thanks