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 1gukHL-0000B4-Ju for pgsql-general@arkaria.postgresql.org; Fri, 15 Feb 2019 20:40:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gukGL-0001wu-4h for pgsql-general@arkaria.postgresql.org; Fri, 15 Feb 2019 20:39:09 +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 1gukGK-0001uv-Ph for pgsql-general@lists.postgresql.org; Fri, 15 Feb 2019 20:39:08 +0000 Received: from mail.appl-ecosys.com ([50.126.108.78]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gukGD-0001tu-WB for pgsql-general@lists.postgresql.org; Fri, 15 Feb 2019 20:39:07 +0000 Received: from salmo.appl-ecosys.com (salmo.appl-ecosys.com [192.168.55.1]) by mail.appl-ecosys.com (Postfix) with ESMTP id E1E7099356 for ; Fri, 15 Feb 2019 12:38:59 -0800 (PST) Date: Fri, 15 Feb 2019 12:38:59 -0800 (PST) From: Rich Shepard To: pgsql-general@lists.postgresql.org Subject: Re: Subquery to select max(date) value In-Reply-To: <87va1oplgl.fsf@news-spur.riddles.org.uk> Message-ID: References: <2780074d-3b87-b154-8bc6-a0f898fbdde2@openvistas.net> <8b99480a-7711-694e-a1c5-ae65bb0c0364@aklaver.com> <87va1oplgl.fsf@news-spur.riddles.org.uk> User-Agent: Alpine 2.20 (LNX 67 2015-01-07) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Wed, 13 Feb 2019, Andrew Gierth wrote: > You want LATERAL. Andrew, et al,: I found a couple of web pages describing the lateral join yet have not correctly applied them. The manual's page did not help me get the correct syntax, either. Think I'm close, however: select p.person_id, p.lname, p.fname, p.direct_phone, o.org_name, a.next_contact from people as p, organizations as o lateral (select a.next_contact from activities as a where a.next_contact is not null and a.next_contact <= 'today' and a.next_contact > '2018-12-31' order by person_id,next_contact); When run I get this syntax error: $ psql -f get_next_contact_dates.sql -d bustrac psql:get_next_contact_dates.sql:10: ERROR: syntax error at or near "lateral" LINE 3: lateral What am I still missing? Regards, Rich