Re: How to select the last value/row?

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Shaozhong SHI <shishaozhong(at)gmail(dot)com>
Cc: pgsql-sql <pgsql-sql(at)lists(dot)postgresql(dot)org>
Subject: Re: How to select the last value/row?
Date: 2023-07-06 16:09:36
Message-ID: CAKFQuwZuhDQ3GsQLreBB15SvehBRz_iJhFoo-qxgtAgmiEXSOQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Thu, Jul 6, 2023 at 9:02 AM Shaozhong SHI <shishaozhong(at)gmail(dot)com> 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.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2023-07-06 16:12:01 Re: How to select the last value/row?
Previous Message Shaozhong SHI 2023-07-06 16:02:40 Re: How to select the last value/row?