Selecting exactly one row for each column value

Поиск
Список
Период
Сортировка
От Florian Weimer
Тема Selecting exactly one row for each column value
Дата
Msg-id 82hcsy4do7.fsf@mid.bfk.de
обсуждение исходный текст
Ответы 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>)
Список 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 по дате отправления:

Предыдущее
От: "Shavonne Marietta Wijesinghe"
Дата:
Сообщение: Re: convert to a string
Следующее
От: "A. Kretschmer"
Дата:
Сообщение: Re: Selecting exactly one row for each column value