pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: Rich Shepard <rshepard@appl-ecosys.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Subquery to select max(date) value
Date: Fri, 15 Feb 2019 21:56:46 +0000
Message-ID: <874l94lo43.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <alpine.LNX.2.20.1902151327540.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.1902151327540.31662@salmo.appl-ecosys.com>

>>>>> "Rich" == Rich Shepard <rshepard@appl-ecosys.com> writes:

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

Yes.

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

The problem here is that you have no join conditions at all, so the
result set of this query is massive. And you've duplicated many tables
inside the subquery which is not necessary or appropriate.

select p.person_id, p.lname, p.fname, p.direct_phone, o.org_name, sq.*
  from people as p
       join organizations as o on p.organization_id=o.id   -- OR WHATEVER
       cross join
         lateral (select a.next_contact
                    from activities as a
                   where a.person_id=p.person_id         --VERY IMPORTANT
                     and a.next_contact > '2018-12-31'
                     and a.next_contact <= 'today'
                     and a.next_contact is not null
                   order by a.next_contact DESC
                   limit 1) sq;

Ordering by DESC with a limit 1 is used to get the max next_contact
value rather than the smallest; this is similar to max(), but makes it
trivial to also access the other columns of the _same_ activities row
which is being selected.

-- 
Andrew (irc:RhodiumToad)




view thread (48+ messages)  latest in thread

Message-ID: <874l94lo43.fsf@news-spur.riddles.org.uk>
Permalink:  ../874l94lo43.fsf@news-spur.riddles.org.uk/
Also on:    postgresql.org/message-id/874l94lo43.fsf@news-spur.riddles.org.uk

 · 

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: andrew@tao11.riddles.org.uk, rshepard@appl-ecosys.com
  Subject: Re: Subquery to select max(date) value
  In-Reply-To: <874l94lo43.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> <alpine.LNX.2.20.1902151327540.31662@salmo.appl-ecosys.com> <874l94lo43.fsf@news-spur.riddles.org.uk>

* 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