pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Rich Shepard <rshepard@appl-ecosys.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Refining query statement
Date: Thu, 17 Jan 2019 08:41:26 -0800
Message-ID: <68ed1eaa-7a03-fbaf-26e2-a1979b636174@aklaver.com> (raw)
In-Reply-To: <alpine.LNX.2.20.1901170759560.13573@salmo.appl-ecosys.com>
References: <alpine.LNX.2.20.1901150738320.5845@salmo.appl-ecosys.com>
	<9a5976c8-76da-f06d-9840-5dcf8723e38b@gmx.net>
	<alpine.LNX.2.20.1901170759560.13573@salmo.appl-ecosys.com>

On 1/17/19 8:14 AM, Rich Shepard wrote:
> On Tue, 15 Jan 2019, Thomas Kellerer wrote:
> 
>>    select distinct on (C.contact_id) C.contact_id, C.lname, C.fname, 
>> C.direct_phone, O.org_name, A.next_contact
>>    from Contacts as C
>>      join Organizations as O on C.org_id = O.org_id
>>      join Activities as A on C.contact_id = A.contact_id
>>    where A.next_contact <= 'today'
>>      and A.next_contact > '2018-12-31'
>>    order by c.contact_id, a.next_contact DESC;
> 
> Combining this with Adrian's advice to use BETWEEN I have this statement
> that almost works:
> 
> SELECT DISTINCT ON (c.contact_id) c.contact_id, c.lname, c.fname, 
> c.direct_phone, o.org_name, a.next_contact
> FROM Contacts AS c
>    JOIN Organizations AS o ON c.org_id = o.org_id
>    JOIN Activities AS a ON c.contact_id = a.contact_id WHERE 
> next_contact BETWEEN '01/01/2019'::date AND 'today'::date ORDER BY 
> c.contact_id, a.next_contact DESC;
> 
> It fails when the most recent next_contact column in Activities is NULL and
> an earier row has a non-NULL date in the specified range.
> 
> I tried specifying max(a.next_contact) and added GROUP BY, but the result
> set all returned o.org_name columns to the same one.
> 
> The WHERE clause needs to exclude a contact_id where the most current 
> row in
> Activities has NULL for the next_contact column. I've tried a few ideas but
> none work so I need to learn the proper syntax, and I don't find that in
> Rick van der Lans' or Joe Celko's books I have.

Got to thinking more and realized the answer depends on what you want 
the query to produce. Can you let us know what is you are trying to pull 
out with the query?

> 
> Looking forward to learning,
> 
> Rich
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com




view thread (32+ messages)  latest in thread

Message-ID: <68ed1eaa-7a03-fbaf-26e2-a1979b636174@aklaver.com>
Permalink:  ../68ed1eaa-7a03-fbaf-26e2-a1979b636174@aklaver.com/
Also on:    postgresql.org/message-id/68ed1eaa-7a03-fbaf-26e2-a1979b636174@aklaver.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: adrian.klaver@aklaver.com, rshepard@appl-ecosys.com
  Subject: Re: Refining query statement
  In-Reply-To: <68ed1eaa-7a03-fbaf-26e2-a1979b636174@aklaver.com>
  References: <alpine.LNX.2.20.1901150738320.5845@salmo.appl-ecosys.com> <9a5976c8-76da-f06d-9840-5dcf8723e38b@gmx.net> <alpine.LNX.2.20.1901170759560.13573@salmo.appl-ecosys.com> <68ed1eaa-7a03-fbaf-26e2-a1979b636174@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