@OMG_Ponies wees er al op:de vensterfunctie dense_rank()
is wat je nodig hebt - of misschien rank()
. De UPDATE
zou er zo uit kunnen zien:
Testcase:
CREATE TEMP TABLE tbl (
id int
, dollars int
, dollars_rank int
, points int
, points_rank int
);
INSERT INTO tbl VALUES
(1, 20, 1, 35, 1)
,(2, 18, 2, 30, 3)
,(3, 10, 3, 33, 2)
,(4, 10, 3, 33, 2); -- I put a dupe row in to demonstrate ties.
UPDATE-verklaring:
UPDATE tbl
SET dollars_rank = r.d_rnk
, points_rank = r.p_rnk
FROM (
SELECT id
, dense_rank() OVER (ORDER BY dollars DESC) AS d_rnk
, dense_rank() OVER (ORDER BY points DESC) AS p_rnk
FROM tbl
) r
WHERE tbl.id = r.id
Je hebt PostgreSQL 8.4 of hoger . nodig voor vensterfuncties.