Re: Finding Max Value in a Row - Mailing list pgsql-sql

From Viktor Bojović
Subject Re: Finding Max Value in a Row
Date
Msg-id CAJu1cLZmdHVpVv4s+tfzVOh8qETJWkK3oFvpapydby7heC_ZZQ@mail.gmail.com
Whole thread Raw
In response to Finding Max Value in a Row  (Carlos Mennens <carlos.mennens@gmail.com>)
List pgsql-sql
<br /><br /><div class="gmail_quote">On Fri, May 11, 2012 at 9:03 PM, Carlos Mennens <span dir="ltr"><<a
href="mailto:carlos.mennens@gmail.com"target="_blank">carlos.mennens@gmail.com</a>></span> wrote:<br /><blockquote
class="gmail_quote"style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> I have a problem in SQL I
don'tknow how to solve and while I'm sure<br /> there are 100+ ways to do this in ANSI SQL, I'm trying to find the<br
/>most cleanest / efficient way. I have a table called 'users' and the<br /> field 'users_id' is listed as the PRIMARY
KEY.I know I can use the<br /> COUNT function, then I know exactly how many records are listed but I<br /> don't know
whatthe maximum or highest numeric value is so that I can<br /> use the next available # for a newly inserted record.
Sadlythe<br /> architect of this table didn't feel the need to create a sequence and<br /> I don't know how to find the
highestvalue.<br /><br /> Thank you for any assistance!<br /><span class="HOEnZb"><font color="#888888"><br /> --<br />
Sentvia pgsql-sql mailing list (<a href="mailto:pgsql-sql@postgresql.org">pgsql-sql@postgresql.org</a>)<br /> To make
changesto your subscription:<br /><a href="http://www.postgresql.org/mailpref/pgsql-sql"
target="_blank">http://www.postgresql.org/mailpref/pgsql-sql</a><br/></font></span></blockquote></div><br /><br
clear="all"/>can you do it like this (slowest way):<br /><br />insert into tableName(users_id,field1,....,fieldN)<br
/>select<b>max</b>(users_id)+1 as newId, value1,.....,valueN<br />from tableName<br /><br /><br />you can also create
(ifyou have create grants ) some sequence and set it's value to maximal value max(users_id), and then insert by
selectingnextvalue as this<br />nextval('sequenceName'::regclass)<br /><br />also you can alter user_id field of table
users(if permissions are granted) and set default value to be nextval('sequenceName'::regclass)<br /><br />-- <br
/>---------------------------------------<br/>Viktor Bojović<br /> ---------------------------------------<br
/>WhereverI go, Murphy goes with me<br /> 

pgsql-sql by date:

Previous
From: Carlos Mennens
Date:
Subject: Finding Max Value in a Row
Next
From: Wes James
Date:
Subject: Re: Finding Max Value in a Row