Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mGlfL-0000ul-G5 for pgsql-general@arkaria.postgresql.org; Thu, 19 Aug 2021 17:17:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mGlfK-0006uQ-7N for pgsql-general@arkaria.postgresql.org; Thu, 19 Aug 2021 17:17:18 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mGlfJ-0006uG-UZ for pgsql-general@lists.postgresql.org; Thu, 19 Aug 2021 17:17:17 +0000 Received: from mail.appl-ecosys.com ([50.126.108.78]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mGlfD-0003cu-CD for pgsql-general@lists.postgresql.org; Thu, 19 Aug 2021 17:17:17 +0000 Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by mail.appl-ecosys.com (Postfix) with ESMTP id B259B2A0108 for ; Thu, 19 Aug 2021 10:17:08 -0700 (PDT) Date: Thu, 19 Aug 2021 10:17:08 -0700 (PDT) From: Rich Shepard To: pgsql-general@lists.postgresql.org Subject: Re: Selecting table row with latest date In-Reply-To: Message-ID: References: <1704154.1629384082@sss.pgh.pa.us> User-Agent: Alpine 2.20 (LNX 67 2015-01-07) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8BIT List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, 19 Aug 2021, Adrian Klaver wrote: > So take David Johnston's query: > Select distinct on (person_nbr) ….. order by person_nbr, contact_date desc; Adrian, contact_date -------------- 2021-08-17 2019-05-14 2019-05-15 2021-08-17 2018-04-05 2021-08-17 2018-04-05 2021-07-23 2019-04-01 2019-05-10 2019-03-15 2021-08-17 2019-04-16 2019-05-15 2021-08-17 2019-05-29 2021-08-17 2019-05-29 2021-07-23 2021-08-12 ... > and add the next_contact filter: > > Select distinct on (person_nbr) * from contacts where next_contact <= > current_date order by person_nbr, contact_date desc; Select distinct on (p.person_nbr) * from c.contacts from people as p, contacts as c where cnext_contact <= current_date and p.person_nbr = c.person_nbr order by p.person_nbr, c.contact_date desc; $ psql -d bustrac -f test_query.sql psql:test_query.sql:5: ERROR: syntax error at or near "from" LINE 2: from people as p, contacts as c ^ I still am missing quite a bit. Rich