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
Re: Selecting exactly one row for each column value Re: Selecting exactly one row for each column value |
Список | 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 по дате отправления: