From: Randall Lucas <rlucas@tercent.net>
To: pgsql-sql@postgresql.org
Subject: Ordinal value of row within set returned by a query?
Date: Thu, 17 Apr 2003 14:01:25 -0400
Message-ID: <9AE98E9A-70FE-11D7-9BCD-000A957653D6@tercent.net> (raw)
Hi folks,
I'm puzzling over whether it is possible within SQL alone to determine
the ordinal position of a row within the set returned by a query. It
seems clear to me that pgsql "knows" what position in a set a
particular tuple holds, since one can OFFSET, ORDER BY, and LIMIT;
however, I can't seem to find a function or "hidden field" that will
return this.
What I would like is something along these lines: I wish to ORDER BY
an ordinal field that is likely to be present, but may not be present,
and then by a unique value to ensure stability of ordering. Since the
ordinal may be absent, I'd like a running total on the side: For
example:
guys
name age
-------------------
bob 33
charlie 35
doug 28
ed 33
select name, age, running_total() from guys order by age, name
name age running_total()
-----------------------------------------
doug 28 1
bob 33 2
ed 33 3
charlie 35 4
I think I could do this by means of creating a temporary table within a
plpgsql function, but that seems awful heavy-duty. I have an inkling
that there may be an existing pg_ function or field, like oid, which I
might call to get this info. Thoughts?
Regards,
Randall
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: rlucas@tercent.net
Subject: Re: Ordinal value of row within set returned by a query?
In-Reply-To: <9AE98E9A-70FE-11D7-9BCD-000A957653D6@tercent.net>
References: <9AE98E9A-70FE-11D7-9BCD-000A957653D6@tercent.net>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox