Solved

Problem with SQL SELECT MAX

Posted on 2012-03-15
12
459 Views
Last Modified: 2012-03-15
Hi,

I'm trying to get the highest customerID number from a table so I can add 1 and have the next number to add a new record.

using "Select MAX (customerID) FROM T_Customers;"

I get a number that is not correct.  The highest customerID number is 11, but this query returns 9.

Thanks
0
Comment
Question by:taborrg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
12 Comments
 
LVL 2

Expert Comment

by:costaf
ID: 37724318
that is wierd, the syntax of the sql command seems to be correct. are you sure ?
what kind of database are you using? where did you run this command?
0
 
LVL 1

Author Comment

by:taborrg
ID: 37724333
Yes - I've been retrying if for over an hour.  

When I try substituting other fields, it works fine.  Only on the primary key does it not work.

Select   MIN(customerID) FROM T_Customers  gives a "1" as it should.

I even rebooted.
0
 
LVL 2

Expert Comment

by:costaf
ID: 37724341
have you or anyone else made any changes to that table , that not have been commited yet?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:taborrg
ID: 37724349
No.

Its a pretty simple setup - just me.
0
 
LVL 1

Author Comment

by:taborrg
ID: 37724371
When I recreate the table with only 5 records it works ok.
0
 
LVL 2

Expert Comment

by:costaf
ID: 37724381
what kind of data  type is associated with the custtomerid ? try to add more data and try again.
0
 
LVL 1

Author Comment

by:taborrg
ID: 37724387
did that.  Many times.
0
 
LVL 1

Author Comment

by:taborrg
ID: 37724392
wait .. might be a type issue.
0
 
LVL 9

Expert Comment

by:bharrington83
ID: 37724401
Sounds like you are using a character field as opposed to a numeric field.  It would sort
1
10
11
12
2
3
4
5
7
8
9
0
 
LVL 2

Accepted Solution

by:
costaf earned 500 total points
ID: 37724406
i aggre with the comment of bharrington83. thats why i asked what data type was associated with the field used in the select max querie.
0
 
LVL 1

Author Comment

by:taborrg
ID: 37724407
Yep.  That's it

It's working now.
0
 
LVL 1

Author Closing Comment

by:taborrg
ID: 37724414
costaf,

You did put me onto the solution -  Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question