Re: Selecting exactly one row for each column value

From: Stefan Becker <pgsql(at)yukonho(dot)de>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Selecting exactly one row for each column value
Date: 2007-03-06 18:14:49
Message-ID: 200703061914.50151.pgsql@yukonho.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Am Dienstag, 6. März 2007 16:03 schrieb Florian Weimer:

> a | b | c
> ---+---+---
> 5 | 6 | 7
> 2 | 3 | 4
> 1 | 2 | 3

Hi,
couldn't you accomplish this by:

select distinct on (a) * from tablename order by a;

here:

create table tab (a int,b int,c int);
insert into tab values (1,2,3);
insert into tab values (5,6,7);
insert into tab values (1,2,3);
insert into tab values (2,3,4);
insert into tab values (1,2,2);
insert into tab values (2,3,4);

select * from tab;
a | b | c
---+---+---
1 | 2 | 3
5 | 6 | 7
1 | 2 | 3
2 | 3 | 4
1 | 2 | 2
2 | 3 | 4
(6 rows)

select distinct on (a) * from tab order by a;
a | b | c
---+---+---
1 | 2 | 3
2 | 3 | 4
5 | 6 | 7
(3 rows)

my regards,

Stefan

--
email: stefan(at)yukonho(dot)de
tel : +49 (0)6232-497631
http://www.yukonho.de

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Oleg Bartunov 2007-03-06 18:57:23 Re: GiST index question: performance
Previous Message Richard Huxton 2007-03-06 18:08:29 Re: Inc