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 1gtgTA-0004La-1I for pgsql-general@arkaria.postgresql.org; Tue, 12 Feb 2019 22:24:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gtgT8-0001rR-B9 for pgsql-general@arkaria.postgresql.org; Tue, 12 Feb 2019 22:23:58 +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 1gtgT7-0001rK-WF for pgsql-general@lists.postgresql.org; Tue, 12 Feb 2019 22:23:58 +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 1gtgT1-0004sR-7M for pgsql-general@lists.postgresql.org; Tue, 12 Feb 2019 22:23:56 +0000 Received: from salmo.appl-ecosys.com (salmo.appl-ecosys.com [192.168.55.1]) by mail.appl-ecosys.com (Postfix) with ESMTP id C674F99356 for ; Tue, 12 Feb 2019 14:23:49 -0800 (PST) Date: Tue, 12 Feb 2019 14:23:49 -0800 (PST) From: Rich Shepard Reply-To: Rich Shepard To: pgsql-general@lists.postgresql.org Subject: Subquery to select max(date) value Message-ID: User-Agent: Alpine 2.20 (LNX 67 2015-01-07) MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset=US-ASCII List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk The query is to return the latest next_contact date for each person. Using the max() aggregate function and modeling the example of lo_temp on page 13 of the rel. 10 manual I wrote this statement: select (P.person_id, P.lname, P.fname, P.direct_phone, O.org_name, A.next_contact) from People as P, Organizations as O, Activities as A where P.org_id = O.org_id and P.person_id = A.person_id and A.next_contact = select (max(A.next_contact) from A) group by A.next_contact, P.person_id; The syntax error returned by psql is: psql:next_contact_date.sql:7: ERROR: syntax error at or near "select" LINE 4: A.next_contact = select (max(A.next_contact) from A) ^ and I fail to see what I've done incorrectly. Do I need to insert DISTINCT ON in the main or sub query? If so, what is the correct syntax to extract all desired columns from each selected row? If this is covered in the manual please point me to the proper section; if not, please educate me on the appropriate syntax to produce the desired output. TIA, Rich