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 1t86d7-0024Sw-36 for pgsql-general@arkaria.postgresql.org; Mon, 04 Nov 2024 23:37:04 +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 1t86d4-006M1C-Ci for pgsql-general@arkaria.postgresql.org; Mon, 04 Nov 2024 23:37:02 +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 1t86cd-006HzL-1y for pgsql-general@lists.postgresql.org; Mon, 04 Nov 2024 23:36:35 +0000 Received: from mout-u-107.mailbox.org ([80.241.59.207]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t86cZ-000Dqa-MK for pgsql-general@postgresql.org; Mon, 04 Nov 2024 23:36:35 +0000 Received: from smtp202.mailbox.org (smtp202.mailbox.org [10.196.197.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-u-107.mailbox.org (Postfix) with ESMTPS id 4Xj7CH42Mmz9sTb; Tue, 5 Nov 2024 00:36:27 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ewie.name; s=MBO0001; t=1730763387; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=IQMEUEZreahamNnNj6H6t1ZahjW0DQtyLdFqstTe7Lg=; b=bPa3Fk2eOdYcBS/Vtuet866dRlpINjTO4LH9zX4BZB4ryasWgj+L9G6iTIB5judqK1RE7C 9eRQ9QHt6z99EiBrHg4Uf+4FZ5FcKjJswMUFRCLSx4kiJKqRdCcUxMHxJX0HtwKnq1pSLf 7adjg+Rj1/CDIVHZTqeYe64K771kgIunenRDyiVQui5WKqiugcqUZ0pNrNcWN0ucI7ngGM +Aup5sHTiV9mirrO52HTYndXzTQL5h+QTn3qtAfYrN2M5UvNivv3fjX2icTgA+/PM9zZ9T hAEbwuk2j6qgQ1n5yC3RNT9w2X4UxGAO+ctpL9SDcqAiktby51DF5U6nW4r8yw== Date: Tue, 5 Nov 2024 00:36:24 +0100 From: Erik Wienhold To: Guyren Howe Cc: PG-General Mailing List Subject: Re: nth_value out of more than n values returns null Message-ID: <56c4c567-8422-4944-81d1-2a3c2ac5c8fa@ewie.name> References: <3ed0759d-c332-4f96-a147-499a694e9204@Spark> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3ed0759d-c332-4f96-a147-499a694e9204@Spark> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2024-11-05 00:17 +0100, 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? https://www.postgresql.org/docs/current/functions-window.html explains it: 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. You probably want to extend the window frame with this: SELECT NTH_VALUE(id, 5000000) OVER ( ORDER BY created_at, id ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) FROM table -- Erik