![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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! -- Sent via pgsql-sql mailing list (pgsql-sql (AT) postgresql (DOT) org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql |
#3
| |||
| |||
|
|
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. |
#4
| |||
| |||
|
|
-----Original Message----- From: pgsql-sql-owner (AT) postgresql (DOT) org [mailto gsql-sql-owner (AT) postgresql (DOT) 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! |
#5
| |||
| |||
|
|
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. |
#6
| |||
| |||
|
#7
| |||
| |||
|
|
Thanks for all the help thus far everyone! I sadly didn't create/design the table and would love to create a SEQUENCE on that particular field but not sure how unless I DROP the table and create from scratch. Currently the data TYPE on the primary key field (users_id) is CHAR and I have no idea why...it should be NUMERIC or SERIAL but it's not so my question is if I want to ALTER the column and create a sequence, would I simply do: ALTER TABLE users ALTER COLUMN users_id TYPE serial ; Obviously if any of the data stored in users_id is actual CHAR, I'm guessing the database would reject that request to change type as the existing data would match. However the data type is CHAR but the field values are all numeric from 1000000010 - 1000000301 so I'm hoping that would work for SERIAL which is just INTEGER, right? |
#8
| |||
| |||
|
|
Thanks for all the help thus far everyone! I sadly didn't create/design the table and would love to create a SEQUENCE on that particular field but not sure how unless I DROP the table and create from scratch. Currently the data TYPE on the primary key field (users_id) is CHAR and I have no idea why...it should be NUMERIC or SERIAL but it's not so my question is if I want to ALTER the column and create a sequence, would I simply do: ALTER TABLE users ALTER COLUMN users_id TYPE serial ; Obviously if any of the data stored in users_id is actual CHAR, I'm guessing the database would reject that request to change type as the existing data would match. However the data type is CHAR but the field values are all numeric from 1000000010 - 1000000301 so I'm hoping that would work for SERIAL which is just INTEGER, right? |
#9
| |||
| |||
|
|
Use this: alter table users * *alter column users_id type integer using to_number(users_id, '99999'); (Adjust the '99999' to the length of the char column) |
#10
| |||
| |||
|
|
Well the question to ask is if it is declared CHAR was that done for a legitimate reason? One reason I can think of is to have leading 0s in a 'number'. Might want to double check that code downstream is not depending on CHAR behavior. |
![]() |
| Thread Tools | |
| Display Modes | |
| |