X-Original-To: pgsql-sql@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 471944758F1 for ; Thu, 17 Apr 2003 14:01:16 -0400 (EDT) Received: from randalls-laptop.kicks-ass.net (h00062577286a.ne.client2.attbi.com [24.61.43.102]) by postgresql.org (Postfix) with ESMTP id 8F539475458 for ; Thu, 17 Apr 2003 14:01:12 -0400 (EDT) Received: from tercent.net (localhost [127.0.0.1]) by randalls-laptop.kicks-ass.net (8.12.7/8.12.6) with ESMTP id h3HI1QVg005299 for ; Thu, 17 Apr 2003 14:01:26 -0400 (EDT) Date: Thu, 17 Apr 2003 14:01:25 -0400 Mime-Version: 1.0 (Apple Message framework v551) Content-Type: text/plain; charset=US-ASCII; format=flowed Subject: Ordinal value of row within set returned by a query? From: Randall Lucas To: pgsql-sql@postgresql.org Content-Transfer-Encoding: 7bit Message-Id: <9AE98E9A-70FE-11D7-9BCD-000A957653D6@tercent.net> X-Mailer: Apple Mail (2.551) X-Spam-Status: No, hits=-8.9 required=5.0 tests=BAYES_10,USER_AGENT_APPLEMAIL autolearn=ham version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) X-Archive-Number: 200304/284 X-Sequence-Number: 12985 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