public inbox for [email protected]  
help / color / mirror / Atom feed
How to select the last value/row?
5+ messages / 3 participants
[nested] [flat]

* How to select the last value/row?
@ 2023-07-06 15:46  Shaozhong SHI <[email protected]>
  0 siblings, 1 reply; 5+ messages in thread

From: Shaozhong SHI @ 2023-07-06 15:46 UTC (permalink / raw)
  To: pgsql-sql <[email protected]>

Given,
ID
5
4
3
3
2
1


How to select the last one, to end up with

1

In Python, there are ways to handle ordered list.

In Postgres, is there something similar?


Regards,

David


^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: How to select the last value/row?
@ 2023-07-06 15:51  David G. Johnston <[email protected]>
  parent: Shaozhong SHI <[email protected]>
  0 siblings, 1 reply; 5+ messages in thread

From: David G. Johnston @ 2023-07-06 15:51 UTC (permalink / raw)
  To: Shaozhong SHI <[email protected]>; +Cc: pgsql-sql <[email protected]>

On Thu, Jul 6, 2023 at 8:47 AM Shaozhong SHI <[email protected]> wrote:

> Given,
> ID
> 5
> 4
> 3
> 3
> 2
> 1
>
>
> How to select the last one, to end up with
>
> 1
>
> In Python, there are ways to handle ordered list.
>
> In Postgres, is there something similar?
>

ORDER BY ASC so the "last item" appears first, then "LIMIT 1"

David J.


^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: How to select the last value/row?
@ 2023-07-06 16:02  Shaozhong SHI <[email protected]>
  parent: David G. Johnston <[email protected]>
  0 siblings, 2 replies; 5+ messages in thread

From: Shaozhong SHI @ 2023-07-06 16:02 UTC (permalink / raw)
  To: David G. Johnston <[email protected]>; +Cc: pgsql-sql <[email protected]>

On Thu, 6 Jul 2023 at 16:51, David G. Johnston <[email protected]>
wrote:

> On Thu, Jul 6, 2023 at 8:47 AM Shaozhong SHI <[email protected]>
> wrote:
>
>> Given,
>> ID
>> 5
>> 4
>> 3
>> 3
>> 2
>> 1
>>
>>
>> How to select the last one, to end up with
>>
>> 1
>>
>> In Python, there are ways to handle ordered list.
>>
>> In Postgres, is there something similar?
>>
>
> ORDER BY ASC so the "last item" appears first, then "LIMIT 1"
>
> David J.
>


How about
ID
5
4
3
3
2
3

The last is 3.

Regards,

David


^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: How to select the last value/row?
@ 2023-07-06 16:09  David G. Johnston <[email protected]>
  parent: Shaozhong SHI <[email protected]>
  1 sibling, 0 replies; 5+ messages in thread

From: David G. Johnston @ 2023-07-06 16:09 UTC (permalink / raw)
  To: Shaozhong SHI <[email protected]>; +Cc: pgsql-sql <[email protected]>

On Thu, Jul 6, 2023 at 9:02 AM Shaozhong SHI <[email protected]> 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.


^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: How to select the last value/row?
@ 2023-07-06 16:12  Tom Lane <[email protected]>
  parent: Shaozhong SHI <[email protected]>
  1 sibling, 0 replies; 5+ messages in thread

From: Tom Lane @ 2023-07-06 16:12 UTC (permalink / raw)
  To: Shaozhong SHI <[email protected]>; +Cc: David G. Johnston <[email protected]>; pgsql-sql <[email protected]>

Shaozhong SHI <[email protected]> writes:
> How about
> ID
> 5
> 4
> 3
> 3
> 2
> 3

> The last is 3.

You need to reorient your thinking.  In SQL, row sets are unordered sets
of values --- this is not a Postgres deficiency, it's a fundamental tenet
of the relational data model.  If you want some kind of ordering, you have
to express that by an ORDER BY clause, which means you need something
within the data that corresponds to what you want the ordering to be.
Your example above is basically nonsense from the standpoint of SQL.

			regards, tom lane






^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2023-07-06 16:12 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2023-07-06 15:46 How to select the last value/row? Shaozhong SHI <[email protected]>
2023-07-06 15:51 ` David G. Johnston <[email protected]>
2023-07-06 16:02   ` Shaozhong SHI <[email protected]>
2023-07-06 16:09     ` David G. Johnston <[email protected]>
2023-07-06 16:12     ` Tom Lane <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox