Re: Subquery to select max(date) value

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Rich Shepard <rshepard(at)appl-ecosys(dot)com>, "pgsql-generallists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Subquery to select max(date) value
Date: 2019-02-13 22:52:22
Message-ID: 0dee994f-120d-06b6-4aea-a10060d2687c@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 2/13/19 2:24 PM, Rich Shepard wrote:
> On Wed, 13 Feb 2019, Andrew Gierth wrote:
>
>> Adrian> Close to your last posted query. person_id 2 and 3 have NULL
>> Adrian> values for activities data as there is no record for 2 and 3 is
>> Adrian> out of the date range.:
>
>> DISTINCT ON with no matching ORDER BY at the _same_ query level is
>> non-deterministic.
>>
>> Also DISTINCT ON isn't efficient. Consider instead something along the
>> lines of:
>
> Andrew/Adrian,
>
> I again read about DISTINCT and DISTINCT ON and fully understand them. I've
> also again read about JOINs; I understand them in terms of sets and _think_
> that in this query the people table is the LEFT (many) while the
> organizations and activities tables are the RIGHT (one) in the many-to-one
> relationships. That is, for each person_id there is only one org_id and
> only
> one next_contact that meets the three constraints.

Given a sufficiently large date range that may not be true as you may
have contacted a given person multiple times during that range and
generated multiple activities records.

>
> I'm now working on understanding how the syntax in the examples you two,
> Ken, and others have provided expresses the many-to-one relationships of
> organization and activities to people. I have the syntax that returns the
> next_date meeting the WHERE constraints to each person_id and am now
> focusing on adding the additional people and organization columns to the
> results. Might not be until tomorrow or Friday but I'll let you and the
> list
> subscribes know when I have understood all your suggestions and get the
> results I want from the query.
>
> Thanks again,
>
> Rich
>
>
>

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Rich Shepard 2019-02-13 23:13:34 Re: Subquery to select max(date) value
Previous Message Maeldron T. 2019-02-13 22:49:55 Streaming replication - invalid resource manager ID