Implementing a total order ranking in PostgreSQL 8.3

后端 未结 3 972
既然无缘
既然无缘 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:46

    If you want a row number equivalent to the window function row_number(), you can improvise in version 8.3 with a (temporary) SEQUENCE:

    CREATE TEMP SEQUENCE foo;
    
    SELECT nextval('foo') AS rn, *
    FROM   (SELECT score FROM tbl ORDER BY score DESC) s
    

    SQL Fiddle.
    The subselect is necessary to order rows before calling nextval().

    Note that the sequence (like any temporary object) ...

    • is only visible in the same session it was created.
    • hides any other table object of the same name.
    • is dropped automatically at the end of the session.

    To use the sequence in the same session repeatedly run before each query:

    SELECT setval('foo', 1, FALSE);
    

提交回复
热议问题