pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Rich Shepard <rshepard@appl-ecosys.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Refining query statement
Date: Tue, 15 Jan 2019 08:34:32 -0800 (PST)
Message-ID: <alpine.LNX.2.20.1901150829570.5845@salmo.appl-ecosys.com> (raw)
In-Reply-To: <9a5976c8-76da-f06d-9840-5dcf8723e38b@gmx.net>
References: <alpine.LNX.2.20.1901150738320.5845@salmo.appl-ecosys.com>
	<9a5976c8-76da-f06d-9840-5dcf8723e38b@gmx.net>

On Tue, 15 Jan 2019, Thomas Kellerer wrote:

> With regards to "cleaner": the first thing to do is to remove the
> parentheses around the column list. In Postgres "(a,b,c)" creates a single
> column with an anonymous record type (that contains three fields), rather
> than selecting three columns. In other DBMS those parentheses are simply
> useless.

Thomas,

I should have seen that myself. Thanks for pointing out the obvious to me.

> "cleaner" is always subjective, but I find explicit JOIN operators a lot
> cleaner than the old implicit joins.

   This does make sense; I need to refresh my knowledge of JOIN operators and
will do so.

> The condition "A.next_contact is not null" is actually no necessary
> because you already have a condition on that column, so NULL values won't
> be returned anyway.

   True that.

> To get the "most recent one" in Postgres, DISTINCT ON () is usually the
> best way to do it: So we end up with something like this:
>
>    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;

   This insight really helps. Now I know how to approach other queries.

Many thanks,

Rich




view thread (32+ messages)  latest in thread

Message-ID: <alpine.LNX.2.20.1901150829570.5845@salmo.appl-ecosys.com>
Permalink:  ../alpine.LNX.2.20.1901150829570.5845@salmo.appl-ecosys.com/
Also on:    postgresql.org/message-id/alpine.LNX.2.20.1901150829570.5845@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: Refining query statement
  In-Reply-To: <alpine.LNX.2.20.1901150829570.5845@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.1901150829570.5845@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