Tip & How-To about Microsoft SQL Server Enterprise Edition for PC

Getting the highest value in a column less than some other value

This is good really for SQL Server, Oracle, or anything that can handle SQL.

Most hacks know about MAX(), as in:

SELECT MAX(col_name) FROM table

So if the column has a max value in it of 10, you get 10 back. But what if you want the highest value in the column that is less than a certain amt.? This for example is good for getting back the next most recent record when col_name is an incrementing key column and you want to exclude the most recent record (ID'd by it having the current maximum value key) in the column. Now you are faced with writing an SP or egad, traversing a recordset client-side. Ugh.

Here's the fix:

SELECT MAX(col_name) FROM table WHERE col_name < {some_value}

This works. It works even when the key column is not guaranteed to have continuous sequential values, such as when records are added and deleted over time and the key values assigned all stay the same (which is what happens in 99.9999999+% of all cases).

Have fun.

Homework: Haven't tried it but I am betting you can also do this:

SELECT MAX(col_name) FROM table WHERE col_name < MAX(col_name)

Whoa... well, try it and see if it works. Some SQL interpreters don't like aggregation/sub-computational functions on the right side of the comparator. In that case, you'd get MAX(col_name) first with a simple SELECT stmt., assign it to a variable, then use that variable as the right hand side modifier for the '<' comparitor.

Seems simple, right? But oh, the pain these kinds of situations cause. Hope this helps some of y'alls.

Posted by on

Microsoft SQL Server Enterprise Edition for PC Logo

Related Topics:

Related Questions:

3 Answers

'Table does not exist' error in MySQL, although it really exists


it usually means that no table exists in the default database with the given name. In some cases, it may be that the table does exist but that you are referring to it incorrectly. Maybe your database and table names are case sensitive if they are located on a file system that has case-sensitive file names. Or perhapsall references to a given table within a query must use the same lettercase.
Use SHOW TABLES to check which tables are in the default database.
Well, if you are sure that table is exist and you write its name exactly such it should be, then I suppose something wrong with the database, it can be seriously corrupt. And in this case you need a professional help. This tool must help at any rate - Recovery Toolbox for MySQL https://mysql.recoverytoolbox.com/

Mar 11, 2015 | Oracle MySQL Enterprise Ed 1-4 Socket...

1 Answer

what is sql condition in visual basic?


sql is used for databases. Something like sql server, oracle, or access uses sql. It is a group of tables configured to reference eachother, so if you are programming in visual basic, you are trying to reference something on your sql server.

Feb 28, 2011 | Microsoft Xbox 360 Console

1 Answer

Oracle SQL Error


Does it connect after the error ?. First try to connect from your client to Oracle server using SQL plus client.

Second - Do a tnsping <server name>

This will give you the correct Host name, SID and Port number.

Make sure that your JDBC connection parameters match with the actual connection from TNS output

Sep 04, 2008 | Oracle 10g Database Standard (ODBSEONUPP0)

3 Answers

Viewing ms Sql Server database on Access


Are you familiar with "Linking" tables in Access? In Access: File --> Get External Data --> Link Tables. This will bring up a dialog box which allows you to browse to your SQL server and select the table you want.

Aug 23, 2007 | Microsoft SQL Server Standard Edition for...

Not finding what you are looking for?

508 people viewed this tip

Ask a Question

Usually answered in minutes!

Top Microsoft Computers & Internet Experts

micky dee

Level 3 Expert

2888 Answers

Piyal Perera
Piyal Perera

Level 3 Expert

528 Answers

Les Dickinson
Les Dickinson

Level 3 Expert

18409 Answers

Are you a Microsoft Computer and Internet Expert? Answer questions, earn points and help others

Answer questions

Loading...