SQL SELECT LAST does not return the last record

Posted on 2011-10-19
Medium Priority
Last Modified: 2012-06-21
I have some SQL running on an ASP page that uses the SELECT LAST query, to select the last record of a few fields where a condition on a certain field/s is true (from an MS Access database).

Up until recently, this was working fine. But I am now seeing some odd behaviour. The query is not selecting the last record for the given condition, neither is it necessarily selecting the first. Sometimes it may be the second (based on the ID field - primary key) record that meets the condition, etc.

As a workaround, I have used SELECT MAX on the ID field where the conditions are true, and run another query based on this result but this is a messy way of doing this based on the rest of my ASP page.

Any help appreciated!
Question by:noooodlez
LVL 39

Accepted Solution

Pratima Pharande earned 1000 total points
ID: 36992294
select top 1 * from table_name order by columnname desc

if the columnname is id of the table

if you want to add your condtion put the where condition

select top 1 * from table_name
where col_anme= 'ss'
order by columnname desc

Author Closing Comment

ID: 36993529
Spot on, worked perfectly. Many thanks!

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

809 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