pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Rich Shepard <rshepard@appl-ecosys.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Subquery to select max(date) value
Date: Fri, 15 Feb 2019 13:07:28 -0800 (PST)
Message-ID: <alpine.LNX.2.20.1902151303280.31662@salmo.appl-ecosys.com> (raw)
In-Reply-To: <878syglqvp.fsf@news-spur.riddles.org.uk>
References: <alpine.LNX.2.20.1902121414490.15295@salmo.appl-ecosys.com>
	<2780074d-3b87-b154-8bc6-a0f898fbdde2@openvistas.net>
	<alpine.LNX.2.20.1902121443290.15295@salmo.appl-ecosys.com>
	<alpine.LNX.2.20.1902121446460.15295@salmo.appl-ecosys.com>
	<8b99480a-7711-694e-a1c5-ae65bb0c0364@aklaver.com>
	<alpine.LNX.2.20.1902121500240.15295@salmo.appl-ecosys.com>
	<87va1oplgl.fsf@news-spur.riddles.org.uk>
	<alpine.LNX.2.20.1902151230581.31662@salmo.appl-ecosys.com>
	<878syglqvp.fsf@news-spur.riddles.org.uk>

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. Don't
> think of LATERAL as being a type of join, think of it as qualifying the
> (SELECT ...) that follows.

Andrew,

Thank you. Now I understand the difference.

> Rich>         from activities as a
> Rich>         where a.next_contact is not null and a.next_contact <= 'today' and
> Rich>               a.next_contact > '2018-12-31'
>
> You'd want a condition here that references the "people" table; the whole
> point of LATERAL is that it opens up the scope of column references in the
> subquery to include those tables which are to its left in the from-clause.

And here I got it backwards, thinking the subquery could reference the
columns in the people table from the initial select.

> Rich>         order by person_id,next_contact);
>
> 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.

That's true. With 'discrete on' only the most recent next_contact date is
returned.

More work over the weekend on this now I have a better understanding of
lateral.

Thanks again,

Rich




view thread (48+ messages)  latest in thread

Message-ID: <alpine.LNX.2.20.1902151303280.31662@salmo.appl-ecosys.com>
Permalink:  ../alpine.LNX.2.20.1902151303280.31662@salmo.appl-ecosys.com/
Also on:    postgresql.org/message-id/alpine.LNX.2.20.1902151303280.31662@salmo.appl-ecosys.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: rshepard@appl-ecosys.com
  Subject: Re: Subquery to select max(date) value
  In-Reply-To: <alpine.LNX.2.20.1902151303280.31662@salmo.appl-ecosys.com>
  References: <alpine.LNX.2.20.1902121414490.15295@salmo.appl-ecosys.com> <2780074d-3b87-b154-8bc6-a0f898fbdde2@openvistas.net> <alpine.LNX.2.20.1902121443290.15295@salmo.appl-ecosys.com> <alpine.LNX.2.20.1902121446460.15295@salmo.appl-ecosys.com> <8b99480a-7711-694e-a1c5-ae65bb0c0364@aklaver.com> <alpine.LNX.2.20.1902121500240.15295@salmo.appl-ecosys.com> <87va1oplgl.fsf@news-spur.riddles.org.uk> <alpine.LNX.2.20.1902151230581.31662@salmo.appl-ecosys.com> <878syglqvp.fsf@news-spur.riddles.org.uk> <alpine.LNX.2.20.1902151303280.31662@salmo.appl-ecosys.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox