?
Solved

Retrieve last record in field SQL query Select statement

Posted on 2010-09-14
6
Medium Priority
?
858 Views
Last Modified: 2012-05-10
I’m trying to write a SELECT statement that brings back the last record of a field (“RECORD”).

Would it look like this?:

Recordset1.Source = “SELECT TOP 1 * FROM JAX ORDER BY RECORD DESC”

Any assistance is greatly appreciated.

Thank you.
0
Comment
Question by:arendt73
[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
6 Comments
 
LVL 7

Assisted Solution

by:tlovie
tlovie earned 600 total points
ID: 33677450
yes
0
 
LVL 42

Accepted Solution

by:
pcelba earned 800 total points
ID: 33677584
This really depends on the order definition.

SELECT TOP 1 * FROM JAX ORDER BY RECORD DESC

retrieves the last record based on the values of RECORD column. Is it correct?

If you need the most recently entered value then you would need to order the query by som etimestamp.

If you need the value which is stored in the physically last DBF record then you should use RECNO() for ordering (this query will work just on DBF file not on SQL Server):

SELECT TOP 1 RECNO(), * FROM JAX ORDER BY 1 DESC

Also the Recordset has MoveLast method, so you could use it ...
0
 

Author Comment

by:arendt73
ID: 33682302
I get the following error when using: SELECT TOP 1 * FROM JAX ORDER BY RECORD DESC

Microsoft OLE DB Provider for ODBC Drivers error '80040e10'

[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.

/statusreports/jax/results-test.asp, line 13

When I mean the last record, I mean the latest entry into the RECORD field.
<%
Dim Recordset1
Dim Recordset1_numRows

Set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = "Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\inetpub\wwwroot\statusreports\db\status.mdb"

Recordset1.Source = "SELECT TOP 1 * FROM JAX ORDER BY RECORD DESC"

Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 1
Recordset1.Open()

Recordset1_numRows = 0
%>

Open in new window

0
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 600 total points
ID: 33682335
if you get that error, it means that RECORD is not a column in the table...
of course, you might try to do this, presuming that RECORD is a reserved keyword:

Recordset1.Source = "SELECT TOP 1 * FROM JAX ORDER BY [RECORD] DESC"

Open in new window

0
 
LVL 42

Expert Comment

by:pcelba
ID: 33682862
If you are entering growing value into the RECORD column then above query should return the latest value.

Access 2007 does not require RECORD to be enclosed in brackets, so what was the problem?
0
 

Author Comment

by:arendt73
ID: 33682890
I forgot to change the name of the field.  My apologizes.
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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 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