Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 94F9017B105B for ; Fri, 11 May 2012 19:01:47 -0300 (ADT) Received: from plane.gmane.org ([80.91.229.3]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SSxuC-0002YG-Fl for pgsql-sql@postgresql.org; Fri, 11 May 2012 22:01:46 +0000 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1SSxty-000304-FD for pgsql-sql@postgresql.org; Sat, 12 May 2012 00:01:30 +0200 Received: from ip-118-90-123-132.xdsl.xnet.co.nz ([118.90.123.132]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 12 May 2012 00:01:30 +0200 Received: from jasen by ip-118-90-123-132.xdsl.xnet.co.nz with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 12 May 2012 00:01:30 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Jasen Betts Subject: Re: Finding Max Value in a Row Date: 11 May 2012 21:59:45 GMT Organization: Dis (not Dat) Organisation Lines: 29 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: ip-118-90-123-132.xdsl.xnet.co.nz X-Face: ?)Aw4rXwN5u0~$nqKj`xPz>xHCwgi^q+^?Ri*+R(&uv2=E1Q0Zk(>h!~o2ID@6{uf8s; a +M[5[U[QT7xFN%^gR"=tuJw%TXXR'Fp~W; (T"1(739R%m0Yyyv*gkGoPA.$b,D.w:z+<'"=-lVT?6 {T?=R^:W5g|E2#EhjKCa+nt":4b}dU7GYB*HBxn&Td$@f%.kl^:7X8rQWd[NTc"P"u6nkisze/Q; 8 "9Z{peQF,w)7UjV$c|RO/mQW/NMgWfr5*$-Z%u46"/00mx-,\R'fLPe.)^ User-Agent: slrn/pre1.0.0-18 (Linux) X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201205/22 X-Sequence-Number: 36570 On 2012-05-11, Carlos Mennens 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