Re: distinct / group by assistance.

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: distinct / group by assistance.
Дата
Msg-id 28895.1214667829@sss.pgh.pa.us
обсуждение исходный текст
Ответ на distinct / group by assistance.  (Gavin 'Beau' Baumanis <gavinb@eclinic.com.au>)
Ответы Re: distinct / group by assistance.  (Gavin 'Beau' Baumanis <gavinb@eclinic.com.au>)
Список pgsql-sql
"Gavin 'Beau' Baumanis" <gavinb@eclinic.com.au> writes:
> ... If there a multiple rows of the same id in table1, I get all  
> (multiple) rows - as you would expect - of course.

> What I need however, is only one row returned per instance a.id that  
> is returned by the above query.

You need GROUP BY a.id.

> I thought of using group by - but there are no calculated fields...  
> and the real query contains 32 fields, which according to the errors I  
> ran into while trying to get this working, would all need to be  
> included in the group by clause.

No, you wouldn't want to do that, because then you'd be back to multiple
rows per a.id value.

The problem here is that for any one a.id value there could be multiple
values of the other variables (coming from different rows) and so the
query results are not well defined if you just add "GROUP BY a.id".
What you need to do is decide which of those values you want and use
an aggregate function to get it.  So your query might end up looking
likeselect a.id, min(a.foo), avg(b.bar), ... from ... group by a.id;
        regards, tom lane


В списке pgsql-sql по дате отправления:

Предыдущее
От: Gavin 'Beau' Baumanis
Дата:
Сообщение: distinct / group by assistance.
Следующее
От: Gavin 'Beau' Baumanis
Дата:
Сообщение: Re: distinct / group by assistance.