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: Tue, 12 Feb 2019 15:20:29 -0800 (PST)
Message-ID: <alpine.LNX.2.20.1902121513500.15295@salmo.appl-ecosys.com> (raw)
In-Reply-To: <CAD3a31U1_V0z9X22wD=1FQ9iYr+KbY8J3NxQpk0Q5Dq5M151fw@mail.gmail.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>
On Tue, 12 Feb 2019, Ken Tanzer wrote:
> select (P.person_id, P.lname, P.fname, P.direct_phone, O.org_name,
> (select max(A.next_contact) from Activities as A WHERE
> p.person_id=A.person_id)
> FROM ...
Ken,
Yes, cheers indeed. A bit of thinking and re-organizing resulted in a
working statement that's close to what I want:
select (P.person_id, P.lname, P.fname, P.direct_phone, O.org_name,
(select max(A.next_contact)))
from People as P, Organizations as O, Activities as A
where P.org_id = O.org_id and P.person_id = A.person_id and
/*A.next_contact = (select max(A.next_contact) from Activities as A) and */
A.next_contact <= 'today' and A.next_contact > '2018-12-31' and
A.next_contact is not null
group by A.next_contact, O.org_id, P.person_id;
The two issues I now focus on resolving are the multiple rows per person
rather than only the most recent and the date displayed at the end of each
output row. DISTINCT ON will eliminate the first issue.
Thanks,
Rich
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.1902121513500.15295@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>
* 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