Re: 'distinct on' and 'order by' conflicts of interest

Поиск
Список
Период
Сортировка
От Stephan Szabo
Тема Re: 'distinct on' and 'order by' conflicts of interest
Дата
Msg-id 20041231093807.U581@megazone.bigpanda.com
обсуждение исходный текст
Ответ на 'distinct on' and 'order by' conflicts of interest  (stephen@thunkit.com)
Список pgsql-general
On Fri, 31 Dec 2004 stephen@thunkit.com wrote:

> It has come up several times on the various postgresql lists that in order
> to get around the requirement of DISTINCT ON parameters matching the first
> ORDER BY parameters, wrap the distinct query in a new 'order by' query:
>
> select * from (select distinct on (a) a,b,c from foo order by a) order by c
>
> however, this will not work when ordering by 'a' will put the wrong record
> first, making it choose the wrong record via distinct on.  The 'order by
> c' superquery no longer has the correct recordset to sort.

I'm not entirely sure from the above which record you want to get from the
distinct on. In general for something like the above, in the subselect you
order by the distinct on column(s) and then the column(s) which control
the which row you want (for example order by a,c). Then you can use the
outer order by to change the ordering to no longer use the distinct on
columns.

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

Предыдущее
От: "Joshua D. Drake"
Дата:
Сообщение: Re: Large Objects
Следующее
От: Tom Lane
Дата:
Сообщение: Re: pg_dump and pgpool