X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 5D7F48B9F81 for ; Fri, 4 Feb 2005 04:21:29 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 57560-08 for ; Fri, 4 Feb 2005 04:21:26 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id 1B8038B9F82 for ; Fri, 4 Feb 2005 04:21:25 +0000 (GMT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.1/8.13.1) with ESMTP id j144LKcx011891 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 3 Feb 2005 21:21:22 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.1/8.13.1) with ESMTP id j144LKco064246; Thu, 3 Feb 2005 21:21:20 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.1/8.13.1/Submit) id j144LJQV064245; Thu, 3 Feb 2005 21:21:19 -0700 (MST) (envelope-from mfuhr) Date: Thu, 3 Feb 2005 21:21:19 -0700 From: Michael Fuhr To: Don Drake Cc: pgsql-sql@postgresql.org Subject: Re: Determining Rank Message-ID: <20050204042119.GA64152@winnie.fuhr.org> References: <6c21003b05020319061f8bce45@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <6c21003b05020319061f8bce45@mail.gmail.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.003 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200502/84 X-Sequence-Number: 20195 On Thu, Feb 03, 2005 at 09:06:36PM -0600, Don Drake wrote: > select some_val, count(*) > from big_table > group by some_val > order by count(*) > limit 50 > > Now, I would like to have the rank included in the result set. The > first row would be 1, followed by 2, etc. all the way to 50. Maybe use a temporary sequence? CREATE TEMPORARY SEQUENCE rank_seq; SELECT nextval('rank_seq') AS rank, * FROM (SELECT some_val, count(*) FROM big_table GROUP BY some_val ORDER BY count(*) LIMIT 50) AS s; DROP SEQUENCE rank_seq; I don't know if row order is guaranteed to survive a subquery, however. -- Michael Fuhr http://www.fuhr.org/~mfuhr/