From: Adrian Klaver <adrian.klaver@aklaver.com>
To: 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 06:48:10 -0800
Message-ID: <fcc4bce0-6e59-d9eb-3644-548b993b84fd@aklaver.com> (raw)
In-Reply-To: <alpine.LNX.2.20.1902130623150.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>
On 2/13/19 6:28 AM, Rich Shepard wrote:
> On Tue, 12 Feb 2019, Ken Tanzer wrote:
>
>> If that's getting you what you want, then great and more power to
>> you. It
>> looks like you'll only get people who have a next_contact in your target
>> window there. You might also consider something like this...
>>
>> select
>> p.person_id,
>> p.lname,
>> p.fname,
>> p.direct_phone,
>> o.org_name,
>> a.next_contact
>> from
>> people as p
>> LEFT JOIN organizations o USING (person_id)
>> LEFT JOIN (
>> SELECT
>> DISTINCT ON (person_id)
>> person_id,
>> next_contact
>> FROM activities a
>> -- WHERE ???
>> ORDER BY person_id,next_contact DESC
>> ) a USING (person_id)
>> ;
>
> I modified this to restrict the time and ignore nulls by replacing the
> question marks:
>
> WHERE a.next_contact <= 'today' and a.next_contact > '2018-12-31' and
> a.next_contact is not null
>
> For a reason I've not yet found, the last condition is not observed; i.e.,
> those rows with null next_contact dates appear in the results. Position in
> the sequence makes no difference. What might cause this?
The LEFT JOIN. There are rows in people for which there no records
coming from the sub-select on activities, so the row is 'padded' with
NULL values for the missing data.
>
> Regards,
>
> Rich
>
>
>
>
--
Adrian Klaver
adrian.klaver@aklaver.com
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: adrian.klaver@aklaver.com, rshepard@appl-ecosys.com
Subject: Re: Subquery to select max(date) value
In-Reply-To: <fcc4bce0-6e59-d9eb-3644-548b993b84fd@aklaver.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>
* 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