Re: "How do I ..." SQL question

From: "Bob" <BHockney(at)ix(dot)netcom(dot)com>
To: PFC <lists(at)boutiquenumerique(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: "How do I ..." SQL question
Date: 2005-01-19 03:11:30
Message-ID: 41ED5F62.1270.30449FEC@localhost
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

PFC wrote:

> Return only four rows beginning at second row:
> >
> > SELECT count(*) AS count, name, year FROM a
> > GROUP BY name, year
> > ORDER BY count DESC, name ASC
> > LIMIT 4 OFFSET 1;
> >
> > count name year
> > ------- ------ ------
> > 3 joe 2004 s,e,e
> > 2 bob 2003 w,e
> > 2 kim 2003 s,s
> > 2 sue 2004 s,w
> >
> > Select only places visited included in LIMITed query:
>
>
> Is this :
>
> SELECT DISTINCT place FROM a,(
> SELECT count(*) AS count, name, year FROM a
> GROUP BY name, year
> ORDER BY count DESC, name ASC
> LIMIT 4 OFFSET 1
> ) as foo WHERE name=foo.name AND year=foo.year
>
> Problem with this approach is that you'll have to run the query twice,
> one to get the hitlist by user, one for the places...
>
> >
> > SELECT DISTINCT place FROM a ????;
> >
> > place
> > -------
> > south
> > west
> > east

Thanks, this worked. As it happens, I am already creating a temporary table
for the hitlist for other uses so that isn't a problem.

-Bob

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Ron Peterson 2005-01-19 03:46:09 Re: mail + rfc822, rfc2822 + schema
Previous Message Bradley Miller 2005-01-18 20:51:01 URGENT: Using function inside query