Re: getting the ranks of items

From: Lyubomir Petrov <lpetrov(at)sysmaster(dot)com>
To: "Randal L(dot) Schwartz" <merlyn(at)stonehenge(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: getting the ranks of items
Date: 2005-05-04 01:13:59
Message-ID: 427821D7.7070302@sysmaster.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Randal L. Schwartz wrote:

>I'm probably asking a FAQ, but a few google searches didn't seem
>to point me in the right place.
>
>Is there a simple way with PostgreSQL to assign relative ranks to the
>result of a query ORDER BY? That is, I want to either have a view
>that cheaply assigns the ranks, or be able to update a column with the
>current ranks (yes, I know this latter version is more prone to
>error).
>
>I'm certain there's probably something I can do to laminate an array
>value to a query result. Am I confused? (Yes!)
>
>
>

Randal,

May be you can use something like this:

create sequence seq_tmp;
select nextval('seq_tmp') as rank, a.id, a.name from (select id, name
from t order by name desc) a;
drop sequence seq_tmp;

I don't know how cheap will this be (because of the sequence), but
couldn't find another way. I do not think that we have something like
Oracle's ROWNUM...

Regards,
Lyubomir Petrov

P.S. I'm sure you can wrap it in plperl stored procedure :)

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Sean Davis 2005-05-04 01:46:17 Re: getting the ranks of items
Previous Message Randal L. Schwartz 2005-05-04 00:57:35 Re: getting the ranks of items