Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id C8D4817B105B for ; Fri, 11 May 2012 16:20:44 -0300 (ADT) Received: from smtp110.prem.mail.ac4.yahoo.com ([76.13.13.93]) by magus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1SSvOK-0005hX-Sx for pgsql-sql@postgresql.org; Fri, 11 May 2012 19:20:43 +0000 Received: (qmail 78863 invoked from network); 11 May 2012 19:20:26 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=DKIM-Signature:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:References:In-Reply-To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:Thread-Index:Content-Language; b=Y5yX8B0wqPGctF3Ch0mWxyDzzBjBrBGPctG/3eCCkhdtKlePFA5cbbmfB7D6O8Ewnff+JUQehXUJfhD9wAnguMYGAUz840qO+1z6VeIk2uragUu0lF857dKUkhveq/bDHw47dZUmXBrxoQsBka/Nd42NzlyU+V7SRGLX2sscDG0= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1336764026; bh=2NwustASRFFrQB/5kWTgjwrzdx60JC1d1Erf6Yxtj1g=; h=X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:References:In-Reply-To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:Thread-Index:Content-Language; b=QPnn4PWUFaI4t0/zIetwHb+3lfxnk65DsVQunIT8qIi+E7qYQt43vE1MEXA99OUuh/rpXPvx95WMYMdYuUzy1BUoH70fTjp0LTLKczvtHzQelkXk8uCgRQZGdBLzJwotQ7IYYmaYKwvPyzenRxj0xQPr5xXYboux0T+ptnZgoK0= X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: Ix7aE2oVM1nHov4Q9i9KY.MXsL9nQm1JVXTvD9tqR3gUod4 YUj4M.FD3am4YE_W0.e2q1ly7biZq34KlupJ0a4DLiMVLRdVGwaWlfFyNTF. 0hTA1ftEYnrysoVzr7dZg5IAnh4kctHxy_.9NV1gd9Lf7Q1_Be.7FYVsv8i3 Y5pmuaGybQVFKV.qmDb22CuRv9ccmMEsPNFXpzZaFUSE0qweiPaoOMPXGsj4 rg7V3o3hHgACZMw5663Cpz5IwdG.Wv5kwQJPVjQIAvkWP7YmnNUxy6a_6rwM Ph.9M3b_9pLekvrU6VRsveYhkHecB2WjGZ58z7CRNpOcTZkjCbbd3KylrjWs VOvSRjzqQggn1EmjlKXSUfpSxrUqM0_lEG2TTvzqr_eORxFMKpFpgPVX4Pb_ QvsS7uO7a_lWMHTBwgsSk X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from WolfDog (polobo@24.93.23.188 with login) by smtp110.prem.mail.ac4.yahoo.com with SMTP; 11 May 2012 12:20:26 -0700 PDT From: "David Johnston" To: "'Carlos Mennens'" , "'PostgreSQL \(SQL\)'" References: In-Reply-To: Subject: Re: Finding Max Value in a Row Date: Fri, 11 May 2012 15:20:16 -0400 Message-ID: <00ee01cd2fab$1b528b80$51f7a280$@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQHh8q1np0mOaPUjOqIwNP6hrPUHXpabb2ZA Content-Language: en-us X-Pg-Spam-Score: -2.0 (--) X-Archive-Number: 201205/11 X-Sequence-Number: 36559 > -----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.