pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Fuhr <mike@fuhr.org>
To: Don Drake <dondrake@gmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Determining Rank
Date: Thu, 3 Feb 2005 21:21:19 -0700
Message-ID: <20050204042119.GA64152@winnie.fuhr.org> (raw)
In-Reply-To: <6c21003b05020319061f8bce45@mail.gmail.com>
References: <6c21003b05020319061f8bce45@mail.gmail.com>

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/



view thread (5+ messages)  latest in thread

Message-ID: <20050204042119.GA64152@winnie.fuhr.org>
Permalink:  ../20050204042119.GA64152@winnie.fuhr.org/
Also on:    postgresql.org/message-id/20050204042119.GA64152@winnie.fuhr.org

 · 

reply

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: mike@fuhr.org, dondrake@gmail.com
  Subject: Re: Determining Rank
  In-Reply-To: <20050204042119.GA64152@winnie.fuhr.org>

* 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