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 1qHRYN-0007J8-UR for pgsql-sql@arkaria.postgresql.org; Thu, 06 Jul 2023 16:09:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1qHRYL-0004Me-W1 for pgsql-sql@arkaria.postgresql.org; Thu, 06 Jul 2023 16:09:57 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qHRYL-0004MV-ND for pgsql-sql@lists.postgresql.org; Thu, 06 Jul 2023 16:09:57 +0000 Received: from mail-ej1-x62c.google.com ([2a00:1450:4864:20::62c]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qHRYJ-002ZRJ-2c for pgsql-sql@lists.postgresql.org; Thu, 06 Jul 2023 16:09:57 +0000 Received: by mail-ej1-x62c.google.com with SMTP id a640c23a62f3a-9923833737eso104114266b.3 for ; Thu, 06 Jul 2023 09:09:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1688659794; x=1691251794; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=NMZsXQrdiuigYHyKKvigJh3swm9fc8wzHmF8T0LXJWs=; b=ZlA4MVuQ5qOdw8IPKPyv3kwxX55ry6Zp/ZxJ/xTTipYKRREE7IgjqCgxjUeVQ1SS09 W5Z2nrOgeAu8UmO8CkgrQcmcKB8cyHrkf2RFgKGP5PPWiG37p8Q0+OejifUoyUhl2j58 T7OWwCwQmVEcDcUZRrpFOffRW2xAzWzj/EAEjJN4dinD9nzeaUm54b6IVgr7G9WHIuhO PyBqQd9wtIC3pHAHO7wxw+gzyysBSvap4kBuV6vct37tQtiw8VkqM8NHjroMHotp62KF vbohe0GCZNJw4mApjhaxGdL8q1Y5YW0nz+AtD0fUNNwJ6dxXwQ0G01xSSzIQZlGPazaY TQCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688659794; x=1691251794; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=NMZsXQrdiuigYHyKKvigJh3swm9fc8wzHmF8T0LXJWs=; b=CGV4qK6cTmQBZvg/x3xIWlFuHGF24K/TeZGcqhDALhWH4oYTTxOljKl66jkyx3UwEr UEg6zwpZILAZ7H5qcEvCwYEVIlpnNHZRoFgLvpaI7/02G5apgXz0NlX3+FA6foTv0Xrb 5NSlaSFlPak/QJVojFP4cJwcCR+5vzai9gqhWsASu2Gzn7Wx2JVSapjZ/+NvjmosZ0sZ gEdDI3ekuCF3cU6PbtK3Oj9qaH6mVYg08vCvTosv2Aw5RM2alMY9LMBKiyHIZ3FtQvgg 4elUmfOFa0tcGbigI7NnmK5pbxqTtIdZ4iQFEeh1fpoghdqLLAE9GAC0asn+SKgCHJwa FthA== X-Gm-Message-State: ABy/qLbbFKTfMhshJz6Na03dIy4xwx38lVYBgNbGC5TTEU5SwPPpSkja XHpLLeWUg0l+iNFsvim6Pk9lHx7jxrQN/Lq+NFQ= X-Google-Smtp-Source: APBJJlE77OU28gRYXdA2ClWpN9+5IaCHEEPCXW9AgiiHbvKuFZ3N6P7IXN0+/lnhfoKS2P0QXMadrWqngB15p07M31k= X-Received: by 2002:a17:906:20dd:b0:989:4952:bf9f with SMTP id c29-20020a17090620dd00b009894952bf9fmr2513163ejc.26.1688659793778; Thu, 06 Jul 2023 09:09:53 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: "David G. Johnston" Date: Thu, 6 Jul 2023 09:09:36 -0700 Message-ID: Subject: Re: How to select the last value/row? To: Shaozhong SHI Cc: pgsql-sql Content-Type: multipart/alternative; boundary="000000000000279ad305ffd3bf52" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000279ad305ffd3bf52 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Jul 6, 2023 at 9:02=E2=80=AFAM Shaozhong SHI wrote: > > How about > ID > 5 > 4 > 3 > 3 > 2 > 3 > > The last is 3. > > There is no concept of "last" in a query unless you define an order. If you really want whatever row really happens to randomly show up in the last result row the use the "row_number()" window function to give each row a number and then sort and limit on that. David J. --000000000000279ad305ffd3bf52 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Thu, Jul 6, 2023 at 9:02=E2=80=AFAM Shaozhong SHI <<= a href=3D"mailto:shishaozhong@gmail.com">shishaozhong@gmail.com> wro= te:

How about=C2=A0
ID
5
=
4
3
3
2
3

=
The last is 3.


<= /div>
There is no concept of "last" in a query unless you defin= e an order.=C2=A0 If you really want whatever row really happens to randoml= y show up in the last result row the use the "row_number()" windo= w function to give each row a number and then sort and limit on that.
=

David J.
--000000000000279ad305ffd3bf52--