pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Rich Shepard <rshepard@appl-ecosys.com>
To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Subquery to select max(date) value [RESOLVED]
Date: Fri, 29 Mar 2019 07:26:43 -0700 (PDT)
Message-ID: <alpine.LNX.2.20.1903290714540.12288@salmo.appl-ecosys.com> (raw)
In-Reply-To: <87mulev72k.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>
	<alpine.LNX.2.20.1903281553550.19962@salmo.appl-ecosys.com>
	<CAKFQuwbQ6f9Kqqs-H+js9hi-9c1Q4pWT9pj=Kq9h3Ld2yVCCoA@mail.gmail.com>
	<alpine.LNX.2.20.1903281617010.19962@salmo.appl-ecosys.com>
	<87mulev72k.fsf@news-spur.riddles.org.uk>

On Fri, 29 Mar 2019, Andrew Gierth wrote:

> That query seems correct assuming you want the result in descending order
> of next_contact. How did the actual result differ from your expectation?

Andrew,

User error: I had a couple of date typos (2018 rather than 2019) and was
thinking of generating the display in ascending, rather than decending,
order.

Thinking again about what I need in terms of query results I made a couple
of changes. Now the query selects the most recent next_contact date >= a
week ago today and into the future:

select p.person_id, p.lname, p.fname, p.direct_phone, p.active, o.org_name, sq.*
from people as p
      join organizations as o on p.org_id = o.org_id
      cross join
          lateral
          (select a.next_contact
          from activities as a
              where a.person_id = p.person_id and
              p.active='True' and
              a.next_contact >= current_date - interval '7' day and
              a.next_contact is not null
          order by a.next_contact ASC
          limit 1) sq
          order by sq.next_contact ASC;

The results are what I need and allow me to not miss a date as long as I run
the query at least once a week.

My thanks to you, David J., and Ken for the valuable lessons.

Best regards,

Rich





view thread (2+ messages)

Message-ID: <alpine.LNX.2.20.1903290714540.12288@salmo.appl-ecosys.com>
Permalink:  ../alpine.LNX.2.20.1903290714540.12288@salmo.appl-ecosys.com/
Also on:    postgresql.org/message-id/alpine.LNX.2.20.1903290714540.12288@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 [RESOLVED]
  In-Reply-To: <alpine.LNX.2.20.1903290714540.12288@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> <874l94lo43.fsf@news-spur.riddles.org.uk> <alpine.LNX.2.20.1903281553550.19962@salmo.appl-ecosys.com> <CAKFQuwbQ6f9Kqqs-H+js9hi-9c1Q4pWT9pj=Kq9h3Ld2yVCCoA@mail.gmail.com> <alpine.LNX.2.20.1903281617010.19962@salmo.appl-ecosys.com> <87mulev72k.fsf@news-spur.riddles.org.uk> <alpine.LNX.2.20.1903290714540.12288@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