Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gjRAy-0008HY-OE for pgsql-general@arkaria.postgresql.org; Tue, 15 Jan 2019 16:02:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gjRAv-0003dL-Te for pgsql-general@arkaria.postgresql.org; Tue, 15 Jan 2019 16:02:49 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gjRAv-0003dD-GE for pgsql-general@lists.postgresql.org; Tue, 15 Jan 2019 16:02:49 +0000 Received: from mail-ot1-x344.google.com ([2607:f8b0:4864:20::344]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gjRAs-00078A-V2 for pgsql-general@lists.postgresql.org; Tue, 15 Jan 2019 16:02:48 +0000 Received: by mail-ot1-x344.google.com with SMTP id i20so2971544otl.0 for ; Tue, 15 Jan 2019 08:02:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=3m1KWXwNSY7C9sNmTLU4LaMX8NsAE2ntM1hNCZu23Rw=; b=u9FNuUTjAHW5tFmFzEZubeqlpIGEBdkDwLp3hoHBLL5XYSaGOXm0+5x5KLuUgafDnC 5poc3C+koJA5irIzaWii3fDw5MNSxGF7inEtTxe7f/nb89B4EuUQv/K8m5Oftukj7keW cdHQ03rNHJjrq27aTp4cF7qFSEEjSbr+0+hOZDIWYDBAdQDaNP/5qZWot8S6CjggZ3Et /s55PenMrVvcqgeswSqE0kky2QSLgHoNa58AM2l6qaenNTopclB/JYbgoPA83siw+TKy ArjnNsbKTNCwLhGxzjmmvfDYLdB7nOzpJCQlbibev88hB2I6qx1m5TIJYkvfa/ib4PM2 GokQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=3m1KWXwNSY7C9sNmTLU4LaMX8NsAE2ntM1hNCZu23Rw=; b=jDaHzgKBd1zWgdxvi2ZPtm/SDB0KEgeG+bA5eOUmO9oWF7e20xSJSH97CJcpl3NROu w04UKU+uHDuENjCeXuhr7Y5+IrvM8lQMOtb65Lc7OJQg5ZsATl3QKo+2trPV+BTDJlJS QCnWXtYCcdUFoGhavhlwjrKDNl41AhTTLj2PKEulwoDWaqdwxts2Ve2tDtfZysgzPTAP 0hzkZGNmIrfLMKw79bWKgScm8iLoOREo/CBa5Pr2/oQJK6tuz7GlFw0cRm3thcKOR178 3ayh3bzMsceQRoyw882203pTWTFzC+dIvRR15WZy5QhWoJZAUQs8Aq5RXg21+nyI0a5w 8+Ig== X-Gm-Message-State: AJcUukdMVWBmLSWMc6IZQDqK4q7m0z0VzaKl63a/RpPaYRu1XlMYl+j5 dD0RPerGAeIIlWP8rMGzs8QWAMeH X-Google-Smtp-Source: ALg8bN7TrnuOo5eMKT/qrMPVxEEoqpyOos1eYYLdIwaOgDrRAEZVgBgp+HyvnwVJA9BRWpO6HPg9kw== X-Received: by 2002:a9d:beb:: with SMTP id 98mr2895399oth.284.1547568165614; Tue, 15 Jan 2019 08:02:45 -0800 (PST) Received: from [192.168.1.10] (ip70-171-116-89.no.no.cox.net. [70.171.116.89]) by smtp.googlemail.com with ESMTPSA id b14sm1456651oti.6.2019.01.15.08.02.44 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 15 Jan 2019 08:02:44 -0800 (PST) Subject: Re: Refining query statement To: pgsql-general@lists.postgresql.org References: <9a5976c8-76da-f06d-9840-5dcf8723e38b@gmx.net> From: Ron Message-ID: <26851021-69e3-36e4-170d-40ed7219a180@gmail.com> Date: Tue, 15 Jan 2019 10:02:43 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: <9a5976c8-76da-f06d-9840-5dcf8723e38b@gmx.net> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 1/15/19 9:47 AM, Thomas Kellerer wrote: > Rich Shepard schrieb am 15.01.2019 um 16:39: >>   Working with my sales/client management system using psql I have a select >> statement to identify contacts to be made. This statement works: >> >> select (C.contact_id, C.lname, C.fname, C.direct_phone, O.org_name, A.next_contact) >> from Contacts as C, Organizations as O, Activities as A >> where C.org_id = O.org_id and C.contact_id = A.contact_id and >>       A.next_contact <= 'today' and A.next_contact > '2018-12-31' and >>       A.next_contact is not null; >> >> but would benefit from tweaking. When I have had multiple contacts with >> someone I want only the most recent one displayed, not all, and they should >> be no more ancient than a defined period (e.g., a month). >> >>   I want to learn how to make this query cleaner and more flexible. When I >> write the UI for this I want to be able to specify a data range in addition >> to a fixed 'today'. Pointers on what to read will be very helpful. > 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. > > "cleaner" is always subjective, but I find explicit JOIN operators a lot cleaner than the old implicit joins. > > 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. > > 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; And I've never liked this method (though I'm old and crotchety).... -- Angular momentum makes the world go 'round.