Implementing a total order ranking in PostgreSQL 8.3

后端 未结 3 978
既然无缘
既然无缘 2021-01-28 12:24

The issue with 8.3 is.....rank is introduced in 8.4.

consider the numbers [10,6,6,2]

I wish to achieve a rank of those numbers where the rank is equal to the th

3条回答
  •  暖寄归人
    2021-01-28 12:29

    There's a method using an array that works with PG 8.3. It's probably not very efficient, performance-wise, but will do OK if there aren't a lot of values.

    The idea is to sort the values in a temporary array, then extract the bounds of the array, then join that with generate_series to extract the values one by one, the index into the array being the row number.

    Sample query assuming the table is scores(value int):

    SELECT i AS row_number,arr[i] AS score
     FROM (SELECT arr,generate_series(1,nb) AS i
       FROM (SELECT arr,array_upper(arr,1) AS nb
         FROM (SELECT array(SELECT value FROM scores ORDER BY value DESC) AS arr
         ) AS s2
       ) AS s1
     ) AS s0
    

提交回复
热议问题