Re: A select DISTINCT query? - followup Q - Mailing list pgsql-general

From Gregory Stark
Subject Re: A select DISTINCT query? - followup Q
Date
Msg-id 878x2aq78x.fsf@oxford.xeocode.com
Whole thread Raw
In response to Re: A select DISTINCT query? - followup Q  (Harald Fuchs <hf0114x@protecting.net>)
List pgsql-general
"Harald Fuchs" <hf0114x@protecting.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!

pgsql-general by date:

Previous
From: Rainer Bauer
Date:
Subject: Re: Is news.postgresql.org down?
Next
From: Sam Mason
Date:
Subject: handling of COUNT(record) vs IS NULL