Skip site navigation (1) Skip section navigation (2)

Re: Finding Max Value in a Row

From: Jasen Betts <jasen(at)xnet(dot)co(dot)nz>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Finding Max Value in a Row
Date: 2012-05-11 21:59:45
Message-ID: jok24h$scd$ (view raw, whole thread or download thread mbox)
Lists: pgsql-sql
On 2012-05-11, 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.

If you need pecisely the next value a sequence won't get you there anyway.

select max(id) from users;

that's only going to be reliable if you have a single concurent database user
doing inserts.

> Thank you for any assistance!

⚂⚃ 100% natural

In response to

pgsql-sql by date

Next:From: John FabianiDate: 2012-05-12 14:28:16
Subject: syncing - between databases
Previous:From: Viktor BojovićDate: 2012-05-11 20:42:38
Subject: Re: Finding Max Value in a Row

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group