Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t86cY-0024OP-BU for pgsql-general@arkaria.postgresql.org; Mon, 04 Nov 2024 23:36:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1t86cU-006G4C-PN for pgsql-general@arkaria.postgresql.org; Mon, 04 Nov 2024 23:36:27 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t86cT-006G43-3q for pgsql-general@lists.postgresql.org; Mon, 04 Nov 2024 23:36:27 +0000 Received: from fhigh-a4-smtp.messagingengine.com ([103.168.172.155]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t86cO-000DqT-2j for pgsql-general@postgresql.org; Mon, 04 Nov 2024 23:36:25 +0000 Received: from phl-compute-09.internal (phl-compute-09.phl.internal [10.202.2.49]) by mailfhigh.phl.internal (Postfix) with ESMTP id B6E9F114010A; Mon, 4 Nov 2024 18:36:18 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-09.internal (MEProxy); Mon, 04 Nov 2024 18:36:18 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm2; t=1730763378; x=1730849778; bh=WO68920IKVeef4g49tSoo691VvKVQ/O8INsKRoTjsJE=; b= tB2TLeF0GJY4c39krhwHufvA5ZOcAUl3fhOPBkI0IBjf+euC/m5iYc2GclF9caXE PBOiarKjAa+dsr/1CWzaGNhEriWmS5a6ioD5r/9QfnDpcx9NB5kw5JK1qTTHElOq mxNNA4e+wDFLN5y8Rryngy6t9gYAFC73oyT82BWqaYni5S2RA3Rad83GwZ1d+QzW TkWIZso7ywI8vOLbGzdhUqvsUDrVJxdSWZeF8oUrBYvlmwewCVAM3RrQBLoWJiz6 h2AwyDt3m9oDmQnQz/Bm1R9EVEhau6jfshvHIYGySKlx7L2gwT7guj7IcAFtaNcb ko9ju4HqbVccwK610ZmTPA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; t=1730763378; x=1730849778; bh=W O68920IKVeef4g49tSoo691VvKVQ/O8INsKRoTjsJE=; b=PTgV0Umh0YFouMPvI xdPwlhxBKfn2l+885OvIvpqegnUtvRkihR18sQMyivymM/bw7/IBhcJTIzmyVaHP 3TD5irkK/eWuTstaPrRw/V+TgUj/pKCVs9sX4RF3AWCDlV0LOqgjobdCbilXu1Ky GBOI+8NV7gFtXqEF1xdLtnSkx5PPhWdS89yOcOIvZBdpnCNVn0569+6NMmS5aR/H kj502QTp9K3Bes7UG7/0yGbV3yX3Px6BTFIM5qzfomwPffB1RhQPUbsEYCE92ho8 //r8QjXBP/Y3zuHc9bSiHoREFueO3IMxmEM15rvl2K2KcpyGgVtlGYjkDI5/eoJ9 hSfIA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdeljedgudefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepkfffgggfuffvfhfhjggtgfesthekredttddvjeen ucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrse grkhhlrghvvghrrdgtohhmqeenucggtffrrghtthgvrhhnpeelgeevkeekkeeuiefgtdev ieeluefhfedufeetkeejffekjeeujeehgeehgeektdenucffohhmrghinhepphhoshhtgh hrvghsqhhlrdhorhhgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghi lhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhdpnhgspg hrtghpthhtohepvddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepghhuhihrvghn sehgmhgrihhlrdgtohhmpdhrtghpthhtohepphhgshhqlhdqghgvnhgvrhgrlhesphhosh htghhrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 4 Nov 2024 18:36:17 -0500 (EST) Message-ID: <50219ea2-3431-403e-b651-8120162370f1@aklaver.com> Date: Mon, 4 Nov 2024 15:36:17 -0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: nth_value out of more than n values returns null To: Guyren Howe , PG-General Mailing List References: <3ed0759d-c332-4f96-a147-499a694e9204@Spark> Content-Language: en-US From: Adrian Klaver In-Reply-To: <3ed0759d-c332-4f96-a147-499a694e9204@Spark> 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 11/4/24 15:17, Guyren Howe wrote: > This query: > > SELECT NTH_VALUE(id, 5000000) OVER (ORDER BY created_at, id ASC) FROM table > > in a table where SELECT COUNT(*) returns a value a few thousand over 5 > million, where id is the primary key, returns null. > > The inclusion of the primary key should make the order by a total order. > So there should be a 5 millionth row. > > How can this happen? If I where to hazard a guess it has to do with this: https://www.postgresql.org/docs/current/functions-window.html "Note that first_value, last_value, and nth_value consider only the rows within the “window frame”, which by default contains the rows from the start of the partition through the last peer of the current row. This is likely to give unhelpful results for last_value and sometimes also nth_value. You can redefine the frame by adding a suitable frame specification (RANGE, ROWS or GROUPS) to the OVER clause. See Section 4.2.8 for more information about frame specifications." Further I am pretty sure that this ORDER BY created_at, id ASC is creating a window frame over created_at, id and that there are duplicate created_at values which means the frame has less then 5000000 rows. Try a smaller number and see what happens. -- Adrian Klaver adrian.klaver@aklaver.com