Re: Q: select query

From: Manfred Koizar <mkoi-pg(at)aon(dot)at>
To: grk(at)usa(dot)net (G(dot) Ralph Kuntz, MD)
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Q: select query
Date: 2003-09-13 18:52:02
Message-ID: s0p6mv8a7bkf7fjsgo4hlcr8co0l1845j7@email.aon.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On 12 Sep 2003 10:58:45 -0700, grk(at)usa(dot)net (G. Ralph Kuntz, MD) wrote:
>I would like to select the second and subsequent rows where the first
>column is the same:
>
> 1 b
> 1 c
> 3 f
>
>in other words, all but the first row of a group.

all = SELECT * FROM t;

but = EXCEPT

the first row of a group =
SELECT i, min(x) FROM t GROUP BY i;

or (if there are more columns)
SELECT DISTINCT ON(i) * FROM t ORDER BY i, x;

Putting it together:
SELECT i, x FROM t EXCEPT (SELECT i, min(x) FROM t GROUP BY i);

or
SELECT * FROM t EXCEPT (SELECT DISTINCT ON(i) ...);

Servus
Manfred

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message chester c young 2003-09-13 19:47:24 sorting
Previous Message Miko O'Sullivan 2003-09-13 18:23:36 Re: [BUGS] session variable