Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 3AB1A1337B9D for ; Wed, 26 Jan 2011 13:02:42 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 07301-02 for ; Wed, 26 Jan 2011 17:02:35 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from jupiter.marktest.pt (delta.marktest.pt [80.251.174.116]) by mail.postgresql.org (Postfix) with ESMTP id A10901337B7A for ; Wed, 26 Jan 2011 13:02:34 -0400 (AST) Received: from Proteu (proteu.marktest.pt [10.61.90.54]) by jupiter.marktest.pt with SMTP (Microsoft Exchange Internet Mail Service Version 5.5.2653.13) id ZQ59K023; Wed, 26 Jan 2011 17:02:33 -0000 Message-ID: From: "Oliveiros d'Azevedo Cristina" To: , References: <4D4047C1.5030208@encs.concordia.ca> Subject: Re: how to get row number in select query Date: Wed, 26 Jan 2011 17:02:33 -0000 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=response Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.5512 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201101/93 X-Sequence-Number: 35040 If it is to order in ascendent fashion by, say, lname, one possibility would be SELECT COUNT(b.*) as row_number, a.lname,a.gname FROM "Table1" a, "Table2" b WHERE a.lname >= b.lname GROUP BY a.lname,a.gname ORDER BY row_number If you want to order by gname just change the WHERE clause accordingly N.B. : This works as long as there is no repetition on the column you use to order. If there is, we'll need a way to tie break. What is your specific case? Also, note that this method is time consuming, and would work only for relatively small tables. AFAIK, version 8.3 doesn't have any "non-standard SQL" way to get a row number, but it is possible that something like that has been introduced in later versions... Best, Oliveiros ----- Original Message ----- From: "Emi Lu" To: Sent: Wednesday, January 26, 2011 4:11 PM Subject: [SQL] how to get row number in select query > Good morning, > > For postgresql 8.3, what is the system method/key word to get row number > please? > > E.g., > > ================== > lname1 gname1 > lname2 gname2 > lname3 gname3 > ...... > > I'd like to get > > 1 lname1 gname1 > 2 lname2 gname2 > 3 lname3 gname3 > > ... ... > > Something like > select row_number?, lname, gname from Table1; > > Thanks a lot! > > > -- > Lu Ying > > -- > Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-sql