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 1mGkLq-0006nc-1c for pgsql-general@arkaria.postgresql.org; Thu, 19 Aug 2021 15:53:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mGkLo-0004kw-P1 for pgsql-general@arkaria.postgresql.org; Thu, 19 Aug 2021 15:53:04 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mGkLm-0004ko-T1 for pgsql-general@lists.postgresql.org; Thu, 19 Aug 2021 15:53:04 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mGkLg-0003cu-7z for pgsql-general@lists.postgresql.org; Thu, 19 Aug 2021 15:53:01 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 0306D5C01F8; Thu, 19 Aug 2021 11:52:53 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Thu, 19 Aug 2021 11:52:53 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=S YWSVSf+tSuK0DIh/XGUfG4WvHiBt/FyPQH84xf8u/c=; b=dRIwS2bdR2de4DCc/ g6YABdQS3/nvzBWbebs8IWUnaqquN1o8fpIoHeRZrqLA27qmxzLyO+tO/z0Ep3/H yAdkMTbUeVI3/yKxpE+wVWFjR7joMjiBmAc6ES3MLj/a0M2KvfGabEsMzYIgBjm0 7tqySypaGTpH8LRAedyAnLl7BhreOAJoas6ZMtZzwDIkQ12JX4IsMsJy/SYBRMhX X1+7sRGKIzUn/uYQ3VoXOyiAjLQqHXpOi3+XWFpvr8pbnen7Hd1gNa3H5BIhVUNF rIlBzQnl1Dq5eetBjk6pXMZuBtD5kpJrisMw4GSqcCF8cyoWh5+Kg9UzXWD50NSq ONJsw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=SYWSVSf+tSuK0DIh/XGUfG4WvHiBt/FyPQH84xf8u /c=; b=mPsVk/jg94NHMS2uNXiQZCTNgW2SqRB8L1wi8mXL22whyjZdO7NrRwoDG PD8xMtOMTthW/c22nvXOda5twaI7E57FoK1d0o6laU1MhAq1V6YWrmpvUW/NQHNi uVm587m3SjzxIprW6xHccXjdrxfU/OcpPsXA2SMu4z3ZojrLW0ey0X+HgNJQbU1x TxYaSewiIr+WT+P89R1XYvSIHuuA7LqoMbuUtQwPx3KgeYpmW5jP8B5Vsorlj0zJ jFG6LPDNaXYfCKSWOV1YIptYdMI+I/cecwUFZu/SRMV8aN5s1fWr2yz91uIFsIQa rE1b4GIe1aAmnViVerwzjv1slq47g== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvtddrleejgdeludcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuggftrfgrthhtvghrnhepveevfeevfeeugeffkeekhfegieeufffgteeujedvgffh ueejjefhkeejleegtdegnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrg hilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 19 Aug 2021 11:52:52 -0400 (EDT) Subject: Re: Selecting table row with latest date To: Rich Shepard , pgsql-general@lists.postgresql.org References: <1704154.1629384082@sss.pgh.pa.us> From: Adrian Klaver Message-ID: Date: Thu, 19 Aug 2021 08:52:51 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 8/19/21 8:39 AM, Rich Shepard wrote: > On Thu, 19 Aug 2021, Tom Lane wrote: > >> 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. > > tom, > > I added an index on contact_date and the query returned only one row. Huh! > Not what I expected. > > This is the script I need to fine-tune (and I've forgotten the role of sq > since someone suggested it a few years ago): > > ---------- > /* This query selects all whose next_contact date is today or earlier; > no nulls. >    This version should select the most recent contact_date by person_nbr, >    order by person_nbr and next_contact date. STILL NEEDS WORK. > */ > > select p.person_nbr, p.lname, p.fname, p.direct_phone, p.cell_phone, > o.company_name, sq.* > from people as p >      join companies as o on p.company_nbr = o.company_nbr >      cross join >          lateral >          (select * >          from contacts as a >              where a.person_nbr = p.person_nbr and >              a.next_contact <= current_date and >              a.next_contact is not null >          order by person_nbr, a.next_contact ASC >          ) sq >          order by sq.next_contact ASC; > ---------- Alright now I am confused. You keep referring to contact_date, yet the query is referring to next_contact. Are they the same thing, different things or other? > > Rich > > -- Adrian Klaver adrian.klaver@aklaver.com