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
Date: Wed, 13 Feb 2019 16:26:56 -0800 (PST)
Message-ID: <alpine.LNX.2.20.1902131623130.10544@salmo.appl-ecosys.com> (raw)
In-Reply-To: <1550101890489.14429@niwa.co.nz>
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>
	<CAD3a31U1_V0z9X22wD=1FQ9iYr+KbY8J3NxQpk0Q5Dq5M151fw@mail.gmail.com>
	<alpine.LNX.2.20.1902121513500.15295@salmo.appl-ecosys.com>
	<CAD3a31VPA6FTLWyQ5KGVXF6ac0y=VG9UaJLEdvyBZWCefrSisw@mail.gmail.com>
	<alpine.LNX.2.20.1902130623150.10544@salmo.appl-ecosys.com>
	<fcc4bce0-6e59-d9eb-3644-548b993b84fd@aklaver.com>
	<alpine.LNX.2.20.1902130736190.10544@salmo.appl-ecosys.com>
	<7c015580-fe20-be68-076c-ded42d8bf1f1@aklaver.com>
	<875ztno1l6.fsf@news-spur.riddles.org.uk>
	<alpine.LNX.2.20.1902131411590.10544@salmo.appl-ecosys.com>
	<0dee994f-120d-06b6-4aea-a10060d2687c@aklaver.com>
	<alpine.LNX.2.20.1902131509560.10544@salmo.appl-ecosys.com>
	<1550101890489.14429@niwa.co.nz>

On Wed, 13 Feb 2019, Brent Wood wrote:

> I have not really followed this thread but would not a query along the lines of
> select * from activity where person_id = n and timestamp = (select
> max(timestamp) from activity where person_id = n);
> give the required answer ie, always return the latest result for the specified person_id??

Brent,

I don't know. What does work is this statement:

SELECT
     DISTINCT ON (person_id) person_id,
     next_contact
     FROM activities AS a
         WHERE a.next_contact is not null and a.next_contact <= 'today' and
               a.next_contact > '2018-12-31'
         ORDER BY person_id,next_contact;

which returns these results:

  person_id | next_contact 
-----------+--------------
          1 | 2019-01-14
          4 | 2019-01-14
         22 | 2019-01-14
         36 | 2019-01-03
         37 | 2019-01-14
         38 | 2019-01-21
         40 | 2019-02-11
         41 | 2019-02-11
         42 | 2019-02-11
         43 | 2019-02-11
         44 | 2019-02-11
         45 | 2019-02-11
         46 | 2019-02-11
(13 rows)

Now I'm learning how to join the people and organization table using LATERAL
join(s) so the results include names and phone numbers.

Thanks for the suggestion,

Rich




view thread (48+ messages)  latest in thread

Message-ID: <alpine.LNX.2.20.1902131623130.10544@salmo.appl-ecosys.com>
Permalink:  ../alpine.LNX.2.20.1902131623130.10544@salmo.appl-ecosys.com/
Also on:    postgresql.org/message-id/alpine.LNX.2.20.1902131623130.10544@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.1902131623130.10544@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> <CAD3a31U1_V0z9X22wD=1FQ9iYr+KbY8J3NxQpk0Q5Dq5M151fw@mail.gmail.com> <alpine.LNX.2.20.1902121513500.15295@salmo.appl-ecosys.com> <CAD3a31VPA6FTLWyQ5KGVXF6ac0y=VG9UaJLEdvyBZWCefrSisw@mail.gmail.com> <alpine.LNX.2.20.1902130623150.10544@salmo.appl-ecosys.com> <fcc4bce0-6e59-d9eb-3644-548b993b84fd@aklaver.com> <alpine.LNX.2.20.1902130736190.10544@salmo.appl-ecosys.com> <7c015580-fe20-be68-076c-ded42d8bf1f1@aklaver.com> <875ztno1l6.fsf@news-spur.riddles.org.uk> <alpine.LNX.2.20.1902131411590.10544@salmo.appl-ecosys.com> <0dee994f-120d-06b6-4aea-a10060d2687c@aklaver.com> <alpine.LNX.2.20.1902131509560.10544@salmo.appl-ecosys.com> <1550101890489.14429@niwa.co.nz> <alpine.LNX.2.20.1902131623130.10544@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