From: | "David Johnston" <polobo(at)yahoo(dot)com> |
---|---|
To: | "'Carlos Mennens'" <carlos(dot)mennens(at)gmail(dot)com>, "'PostgreSQL \(SQL\)'" <pgsql-sql(at)postgresql(dot)org> |
Subject: | Re: Finding Max Value in a Row |
Date: | 2012-05-11 19:20:16 |
Message-ID: | 00ee01cd2fab$1b528b80$51f7a280$@yahoo.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
> -----Original Message-----
> From: pgsql-sql-owner(at)postgresql(dot)org [mailto:pgsql-sql-
> owner(at)postgresql(dot)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.
From | Date | Subject | |
---|---|---|---|
Next Message | Thomas Kellerer | 2012-05-11 19:24:53 | Re: Finding Max Value in a Row |
Previous Message | Wes James | 2012-05-11 19:17:14 | Re: Finding Max Value in a Row |