Re: Finding Max Value in a Row

Поиск
Список
Период
Сортировка
От David Johnston
Тема Re: Finding Max Value in a Row
Дата
Msg-id 00ee01cd2fab$1b528b80$51f7a280$@yahoo.com
обсуждение исходный текст
Ответ на Finding Max Value in a Row  (Carlos Mennens <carlos.mennens@gmail.com>)
Ответы Re: Finding Max Value in a Row  (Carlos Mennens <carlos.mennens@gmail.com>)
Список pgsql-sql
> -----Original Message-----
> From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-
> owner@postgresql.org] On Behalf Of Carlos Mennens
> Sent: Friday, May 11, 2012 3:04 PM
> To: PostgreSQL (SQL)
> Subject: [SQL] Finding Max Value in a Row
> 
> I have a problem in SQL I don't know how to solve and while I'm sure there
> are 100+ ways to do this in ANSI SQL, I'm trying to find the most cleanest
/
> efficient way. I have a table called 'users' and the field 'users_id' is
listed as
> the PRIMARY KEY. I know I can use the COUNT function, then I know exactly
> how many records are listed but I don't know what the maximum or highest
> numeric value is so that I can use the next available # for a newly
inserted
> record. Sadly the architect of this table didn't feel the need to create a
> sequence and I don't know how to find the highest value.
> 
> Thank you for any assistance!
> 

Finding the MAXimium of a given set of data is an aggregate operation and so
you should look in the functions section of the documentation under
"Aggregate"

http://www.postgresql.org/docs/9.0/interactive/functions-aggregate.html

Assuming that the users_id field is an integer:

SELECT MAX(users_id) FROM users; --NO GROUP BY needed since no other fields
are being output...

That said, you really should create and attach a sequence so that you can
avoid race/concurrency issues.

David J.




В списке pgsql-sql по дате отправления:

Предыдущее
От: Wes James
Дата:
Сообщение: Re: Finding Max Value in a Row
Следующее
От: Thomas Kellerer
Дата:
Сообщение: Re: Finding Max Value in a Row