Re: Finding Max Value in a Row

From: Wes James <comptekki(at)gmail(dot)com>
To: Carlos Mennens <carlos(dot)mennens(at)gmail(dot)com>
Cc: "PostgreSQL (SQL)" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Finding Max Value in a Row
Date: 2012-05-11 19:17:14
Message-ID: CAFjCMHsLXjMHQpw_X+2uO9D+LVZx2yKOUzCpmHVSWh=5EwM+YA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Fri, May 11, 2012 at 1:03 PM, Carlos Mennens
<carlos(dot)mennens(at)gmail(dot)com> wrote:
> 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.

select max(row_name) as max_val from table;

Why not create a sequence on the current data then use currval() to
get the max value.

-wes

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message David Johnston 2012-05-11 19:20:16 Re: Finding Max Value in a Row
Previous Message Viktor Bojović 2012-05-11 19:17:07 Re: Finding Max Value in a Row