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 1t87lm-002Eiu-Oz for pgsql-general@arkaria.postgresql.org; Tue, 05 Nov 2024 00:50:06 +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 1t87lj-006zwi-Mg for pgsql-general@arkaria.postgresql.org; Tue, 05 Nov 2024 00:50:04 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t87lj-006zwW-BW for pgsql-general@lists.postgresql.org; Tue, 05 Nov 2024 00:50:03 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t87ld-000DdQ-H9 for pgsql-general@postgresql.org; Tue, 05 Nov 2024 00:50:02 +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 4A50nqDm2936686; Mon, 4 Nov 2024 19:49:52 -0500 From: Tom Lane To: "David G. Johnston" cc: Guyren Howe , Erik Wienhold , PG-General Mailing List Subject: Re: nth_value out of more than n values returns null In-reply-to: References: <3ed0759d-c332-4f96-a147-499a694e9204@Spark> <56c4c567-8422-4944-81d1-2a3c2ac5c8fa@ewie.name> <4e60ede0-86a5-4900-b415-05d68ad75cb1@Spark> Comments: In-reply-to "David G. Johnston" message dated "Mon, 04 Nov 2024 16:44:24 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <2936684.1730767792.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 04 Nov 2024 19:49:52 -0500 Message-ID: <2936685.1730767792@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "David G. Johnston" writes: > So just use =E2=80=9Coffset 5_000_000 limit 1=E2=80=9D. Bringing in a w= indow function here > seems unhelpful. Yeah, that. A bite-size example might help clarify what the window function is doing: regression=3D# create table zed(f1 int) ; CREATE TABLE regression=3D# insert into zed select generate_series(1, 10); INSERT 0 10 regression=3D# select f1, nth_value(f1, 5) over (order by f1) from zed; f1 | nth_value = ----+----------- 1 | = 2 | = 3 | = 4 | = 5 | 5 6 | 5 7 | 5 8 | 5 9 | 5 10 | 5 (10 rows) For the first four rows, the window frame doesn't include the row you want, so you get NULL. You can fix that with a non-default window frame: regression=3D# select f1, nth_value(f1, 5) over (order by f1 ROWS BETWEEN = UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) from zed; f1 | nth_value = ----+----------- 1 | 5 2 | 5 3 | 5 4 | 5 5 | 5 6 | 5 7 | 5 8 | 5 9 | 5 10 | 5 (10 rows) So yeah, you can get the fifth (or five million'th) row this way, but you'll get N copies of it, which I assume is not what you want. Better regression=3D# select f1 from zed order by f1 offset 4 limit 1; f1 = ---- 5 (1 row) which gets you just the one row and is a lot cheaper too. regards, tom lane