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 1t86gt-0024oE-1m for pgsql-general@arkaria.postgresql.org; Mon, 04 Nov 2024 23:40:58 +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 1t86gq-006PGh-Df for pgsql-general@arkaria.postgresql.org; Mon, 04 Nov 2024 23:40:57 +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 1t86gq-006PGY-2b for pgsql-general@lists.postgresql.org; Mon, 04 Nov 2024 23:40:56 +0000 Received: from mail-pf1-x435.google.com ([2607:f8b0:4864:20::435]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1t86gm-000DtP-HT for pgsql-general@postgresql.org; Mon, 04 Nov 2024 23:40:56 +0000 Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-71e4e481692so4699398b3a.1 for ; Mon, 04 Nov 2024 15:40:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730763651; x=1731368451; darn=postgresql.org; h=mime-version:subject:references:in-reply-to:message-id:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=8kWvMYEL2SsWzjA9meYFjUMk351VqT95x0JF6mayDzc=; b=GOKH5dOlam6KP3UObTYDyOe21ecwg/BDLi6Ue3PtqrkR3rCDqj2EAruU9ufBSt+mUg k2NSSpcsdDLeEb6FIxXNycVA97UM5tDppNJ3rn/JbKiibZ9/twJzGjD+P4ooBHw4zD+2 Ba+Omqjq4ZaXRwEkS3EVaZMNWyHmtUhQUEDtR+TImdxYWBHQgqmLX4H8Rug8PRNoryBJ Fw5N1E2LT1OrVqxu7nMS+9MVV3qqKvvEzu34K6YAhik4AgwZc1Nxi+JtCfcWafOiPgz/ VmS7ztQsGwzv3MFqvo+1ygJlGcdyzxowJrrEwaXbrajP9rAbDvDVp6cA3XHaoKztLu/T ykQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730763651; x=1731368451; h=mime-version:subject:references:in-reply-to:message-id:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=8kWvMYEL2SsWzjA9meYFjUMk351VqT95x0JF6mayDzc=; b=HIaECZp0oYkmzsrjHvCThqJ9PhP5maTo9TrjW5VUszHKr4XiHmVfnp7vCLhdOaG/Jz tI349jJD3LTAxtzoAU1j6/cIzOuTTLHS7eY9zyhtkbEFS0ckWRQWuDQqazoFqMZlrr2c KP4VFcd8zEiGv88Qixuw09TxctyDq3IVT27eAXsgaCZF7Xl9ijW1q244W7jdhCZViE5e bz6+i1tgq61+TNacnhFRDi+Fk5ydRwXgSZ6neOjkI/JBGnrSBY2l9JP2qvanfeQYPs6b 2fQIzjHqfw179e8fX8P4JvVlYYddj1DgMsahsfMK9Gv2IycGDfbZ579Xf/+UXketZngm Z88w== X-Gm-Message-State: AOJu0YwCglFL8twgX0mZbyndPmsSFxOKd9d+4mkl6wcJXbM/l2vuBp7T nY6/gNZUphGkmUGRpTaP8eQ2fNIgprsBQ8rWpsMQZaKdKi31lXq6cxcuRtEk X-Google-Smtp-Source: AGHT+IEXJ3UQzxDPwdTnLGGpq0QLn+WvocSvum1cQJhjRIQEuiPdJBnFAZseKdvz1GYeeV7kX2S8nw== X-Received: by 2002:a05:6a21:670f:b0:1d8:a759:524d with SMTP id adf61e73a8af0-1dba52abe75mr21805519637.18.1730763651380; Mon, 04 Nov 2024 15:40:51 -0800 (PST) Received: from [2600:8801:8600:3d2:0:6401:300:0] ([2600:8801:8600:3d2::9]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-720bc2e9203sm8373440b3a.142.2024.11.04.15.40.50 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 04 Nov 2024 15:40:50 -0800 (PST) Date: Mon, 4 Nov 2024 15:38:05 -0800 From: Guyren Howe To: Erik Wienhold Cc: PG-General Mailing List Message-ID: <4e60ede0-86a5-4900-b415-05d68ad75cb1@Spark> In-Reply-To: <56c4c567-8422-4944-81d1-2a3c2ac5c8fa@ewie.name> References: <3ed0759d-c332-4f96-a147-499a694e9204@Spark> <56c4c567-8422-4944-81d1-2a3c2ac5c8fa@ewie.name> Subject: Re: nth_value out of more than n values returns null X-Readdle-Message-ID: 4e60ede0-86a5-4900-b415-05d68ad75cb1@Spark MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="67295b80_484c5e90_11af" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --67295b80_484c5e90_11af Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline I=E2=80=99m trying to get the id of the 5,000,000th record, so I can join= against it to get a name. I didn=E2=80=99t fully understand what the doc= s say there. What am I missing=3F On 4 Nov 2024 at 15:36 -0800, Erik Wienhold , wrote: > On 2024-11-05 00:17 +0100, Guyren Howe wrote: > > This query: > > > > SELECT NTH=5FVALUE(id, 5000000) OVER (ORDER BY created=5Fat, id ASC) = =46ROM 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=3F > > https://www.postgresql.org/docs/current/functions-window.html explains > it: > > Note that first=5Fvalue, last=5Fvalue, and nth=5Fvalue consider only th= e rows > within the =22window frame=22, which by default contains the rows from = the > start of the partition through the last peer of the current row. This i= s > likely to give unhelpful results for last=5Fvalue and sometimes also > nth=5Fvalue. 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=5FVALUE(id, 5000000) OVER ( > ORDER BY created=5Fat, id ASC > ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED =46OLLOWING > ) =46ROM table > > -- > Erik --67295b80_484c5e90_11af Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline
I=E2=80=99m trying to get the id of the 5,000,000th= record, so I can join against it to get a name. I didn=E2=80=99t fully u= nderstand what the docs say there. What am I missing=3F
On 4 Nov 2024 at 15:36 -0800, Erik = Wienhold <ewie=40ewie.name>, wrote:
On 2024-11-05 00:17 +0100, Guyren Howe wrote:
This query:

SELECT NTH=5FVALUE(id, 5000000) OVER (ORDER BY created=5Fat, id ASC) =46R= OM 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=3F

https://www.postgresql.org/docs/current/functions-window.html explains it:

Note that first=5Fvalue, last=5Fvalue, and nth=5Fvalue consider only the = rows
within the =22window frame=22, which by default contains the rows from th= e
start of the partition through the last peer of the current row. This is<= br /> likely to give unhelpful results for last=5Fvalue and sometimes also
nth=5Fvalue. 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=5FVALUE(id, 5000000) OVER (
ORDER BY created=5Fat, id ASC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED =46OLLOWING
) =46ROM table

--
Erik
--67295b80_484c5e90_11af--