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 1mGjEX-0004aa-6u for pgsql-general@arkaria.postgresql.org; Thu, 19 Aug 2021 14:41:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mGjEW-0002NX-4F for pgsql-general@arkaria.postgresql.org; Thu, 19 Aug 2021 14:41:28 +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 1mGjEV-0002NK-RX for pgsql-general@lists.postgresql.org; Thu, 19 Aug 2021 14:41:27 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mGjET-0002J7-Q9 for pgsql-general@lists.postgresql.org; Thu, 19 Aug 2021 14:41:27 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 17JEfMY91704155; Thu, 19 Aug 2021 10:41:23 -0400 From: Tom Lane To: Rich Shepard cc: pgsql-general@lists.postgresql.org Subject: Re: Selecting table row with latest date In-reply-to: References: Comments: In-reply-to Rich Shepard message dated "Thu, 19 Aug 2021 07:37:27 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1704153.1629384082.1@sss.pgh.pa.us> Date: Thu, 19 Aug 2021 10:41:22 -0400 Message-ID: <1704154.1629384082@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Rich Shepard writes: > I have a table of contacts (PK is the person_nbr and contact_date) and I > want to select only the row with the latest (most recent) contact_date. The > Comparison Date/Time sections in the postgres 12 doc doesn't appear to have > what I want, and when I try to use the max() aggregate function it throws an > error. The best way is usually like select * from mytable order by contact_date desc limit 1; If you have an index on contact_date this should work very well indeed. regards, tom lane