@OMG_Ponies sudah menunjukkannya:dense_rank()
rank()
. UPDATE
bisa terlihat seperti ini:
Kasus uji:
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 pernyataan:
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
Anda memerlukan PostgreSQL 8.4 atau yang lebih baru untuk fungsi jendela.