Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id CA7511337B7A for ; Thu, 27 Jan 2011 10:32:18 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 30917-06 for ; Thu, 27 Jan 2011 14:32:11 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from oldperseverance.encs.concordia.ca (oldperseverance.encs.concordia.ca [132.205.96.94]) by mail.postgresql.org (Postfix) with ESMTP id 660301337BA4 for ; Thu, 27 Jan 2011 10:32:11 -0400 (AST) Received: from cacao.encs.concordia.ca (emilu@cacao.encs.concordia.ca [132.205.47.225]) by oldperseverance.encs.concordia.ca (envelope-from emilu@encs.concordia.ca) (8.13.7/8.13.7) with ESMTP id p0REW9kf020836; Thu, 27 Jan 2011 09:32:09 -0500 Message-ID: <4D4181E9.2000305@encs.concordia.ca> Date: Thu, 27 Jan 2011 09:32:09 -0500 From: Emi Lu Reply-To: emilu@encs.concordia.ca User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.16) Gecko/20101125 Lightning/1.0b1 Thunderbird/3.0.11 MIME-Version: 1.0 To: "Oliveiros d'Azevedo Cristina" CC: pgsql-sql@postgresql.org Subject: Re: how to get row number in select query References: <4D4047C1.5030208@encs.concordia.ca> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.58 on oldperseverance.encs.concordia.ca at 2011/01/27 09:32:09 EST 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/103 X-Sequence-Number: 35050 Hi Oliveiros, > 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... Thank you for the answer. I see psql8.4 has the method. I am not interest in ordering any columns, but just a line num. I'd like to get it from DB since displaytag _rowNum does not display row num correctly: 1, 10, 11, 12.... 2, 3, 4, 5, 6... Thanks, -- Lu Ying