Re: Unique - first

From: Marcin Mańk <marcin(at)maniek(dot)info>
To: Robert James <srobertjames(at)gmail(dot)com>
Cc: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Unique - first
Date: 2013-10-27 14:18:39
Message-ID: CAK61fk7b09NMdupaT1KYy+WdDJBqGVT6CH+=5JuKjfkFp+7nxg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sun, Oct 27, 2013 at 2:04 PM, Robert James <srobertjames(at)gmail(dot)com>
wrote:

> I have a table (x,y,z) - I'd like to take the rows with unique x
> values - but, when more than one row have the same x value, I want the
> one with the minimal z value.
>

You can use distinct on (which is a Postgresql extension to the SQL
standard):

select distinct on(x) x, y, z
from the_table
order by x, z

Regards
Marcin Mańk

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2013-10-27 14:26:20 Re: Unique - first
Previous Message Tom Lane 2013-10-27 14:14:58 Re: Segmentation fault: pg_upgrade 9.1 to 9.3: pg_dump: row number 0 is out of range 0..-1