Re: Subquery to select max(date) value

Поиск
Список
Период
Сортировка
От Rich Shepard
Тема Re: Subquery to select max(date) value
Дата
Msg-id alpine.LNX.2.20.1902151327540.31662@salmo.appl-ecosys.com
обсуждение исходный текст
Ответ на Re: Subquery to select max(date) value  (Andrew Gierth <andrew@tao11.riddles.org.uk>)
Ответы Re: Subquery to select max(date) value  (Andrew Gierth <andrew@tao11.riddles.org.uk>)
Список pgsql-general
On Fri, 15 Feb 2019, Andrew Gierth wrote:

> LATERAL (SELECT ...)   is syntactically like (SELECT ...) in that it
> comes _after_ a "," in the from-clause or after a [LEFT] JOIN keyword.

Andrew,

Yes, the missing ',' made a big difference.

> You'd want a condition here that references the "people" table;

Got it.

> and I'm guessing you want that ordered by next_contact alone, possibly
> with LIMIT 1 to get just the nearest following next_contact time.

Using LIMIT 1 produces only the first returned row. This statement (using
max() for next_contact) produces no error message, but also no results so I
killed the process after 30 seconds. Without a syntax error for guidance I
don't know how to proceed. I've not before run 'explain' on a query. Would
that be appropriate here?

select p.person_id, p.lname, p.fname, p.direct_phone, o.org_name
from people as p, organizations as o,
     lateral
         (select p.person_id, p.lname, p.fname, p.direct_phone, o.org_name,
                 max(a.next_contact)
         from people as p, organizations as o, activities as a
         where a.next_contact > '2018-12-31' and
               a.next_contact <= 'today' and
               a.next_contact is not null
         group by p.person_id, o.org_name, a.next_contact
         order by p.person_id, o.org_name, a.next_contact) sq;

Regards,

Rich


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

Предыдущее
От: Thomas Munro
Дата:
Сообщение: Re: WSL (windows subsystem on linux) users will need to turn fsyncoff as of 11.2
Следующее
От: Andrew Gierth
Дата:
Сообщение: Re: Subquery to select max(date) value