Re: A select DISTINCT query? - followup Q

From: Gregory Stark <stark(at)enterprisedb(dot)com>
To: "Harald Fuchs" <hf0114x(at)protecting(dot)net>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: A select DISTINCT query? - followup Q
Date: 2008-01-28 15:50:38
Message-ID: 878x2aq78x.fsf@oxford.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Harald Fuchs" <hf0114x(at)protecting(dot)net> writes:

> If you want to select both columns, but have uniqueness over the first
> only, you can use a derived table:
>
> SELECT tbl.name, tbl.comment
> FROM tbl
> JOIN (SELECT name FROM tbl GROUP BY name HAVING count(*) = 1) AS t
> ON t.name = tbl.name
>

Or use the first() aggregate since you know there's only going to be one
anyways:

select name, first(comment)
from tbl
group by name
having count(*) = 1

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com
Ask me about EnterpriseDB's PostGIS support!

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Sam Mason 2008-01-28 15:59:43 handling of COUNT(record) vs IS NULL
Previous Message Rainer Bauer 2008-01-28 15:24:30 Re: Is news.postgresql.org down?