Re: Help with a subselect inside a view

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: Help with a subselect inside a view
Дата
Msg-id 29633.1124986471@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Re: Help with a subselect inside a view  (Bill Moseley <moseley@hank.org>)
Ответы Re: Help with a subselect inside a view  (Bill Moseley <moseley@hank.org>)
Список pgsql-general
Bill Moseley <moseley@hank.org> writes:
>> http://www.postgresql.org/docs/current/static/sql-select.html#SQL-DISTINCT

>    The DISTINCT ON expression(s) must match the leftmost ORDER BY
>    expression(s). The ORDER BY clause will normally contain additional
>    expression(s) that determine the desired precedence of rows within
>    each DISTINCT ON group.

> I read that and thought it wasn't a drop-in replacement for my code
> due to the leftmost ORDER BY requirement.  But, it seems to work even
> if that requirement is not met.

>     CREATE VIEW cl  (id, class_time, instructor)
>         AS
>             SELECT DISTINCT ON(class.id)
>                    class.id, class.class_time, person.first_name
>               FROM class, instructors, person
>              WHERE instructors.person = person.id
>                AND class.id = instructors.class;

This is allowed because the code automatically adds "ORDER BY class.id"
within the view (as you would see if you examined the view with \d).
It's fairly pointless though, because as the manual notes, you can't get
any well-defined behavior without additional ORDER BY columns to
prioritize the rows within class.id groups.  As is, you're getting
random choices of class_time and first_name within the groups.
(Though maybe in this application, you don't care.)

            regards, tom lane

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

Предыдущее
От: Bill Moseley
Дата:
Сообщение: Re: Help with a subselect inside a view
Следующее
От: Bill Moseley
Дата:
Сообщение: Re: Help with a subselect inside a view