Selecting exactly one row for each column value - Mailing list pgsql-sql

From Florian Weimer
Subject Selecting exactly one row for each column value
Date
Msg-id 82hcsy4do7.fsf@mid.bfk.de
Whole thread Raw
Responses Re: Selecting exactly one row for each column value  ("A. Kretschmer" <andreas.kretschmer@schollglas.com>)
Re: Selecting exactly one row for each column value  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: Selecting exactly one row for each column value  (Stefan Becker <pgsql@yukonho.de>)
List pgsql-sql
I've got the following table:

fweimer=> SELECT * FROM tab;a | b | c
---+---+---1 | 2 | 35 | 6 | 71 | 2 | 22 | 3 | 41 | 2 | 22 | 3 | 4

For each value in the first column, I need one (and only one) matching
row from the table.  A possible solution is:
a | b | c
---+---+---5 | 6 | 72 | 3 | 41 | 2 | 3

Of course,

SELECT a, (SELECT b FROM tab  i  WHERE  i.a = o.a LIMIT 1),     (SELECT c FROM TAB i WHERE i.a = o.a LIMIT 1) FROM tab
oGROUP BY o.a; 

does the trick, but this approach seems to rely on undefined behavior
and quickly gets messy when the number of columns increases.

Is there a better way to implement this?

--
Florian Weimer                <fweimer@bfk.de>
BFK edv-consulting GmbH       http://www.bfk.de/
Kriegsstraße 100              tel: +49-721-96201-1
D-76133 Karlsruhe             fax: +49-721-96201-99


pgsql-sql by date:

Previous
From: "Shavonne Marietta Wijesinghe"
Date:
Subject: Re: convert to a string
Next
From: "A. Kretschmer"
Date:
Subject: Re: Selecting exactly one row for each column value