If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > How to find a record by number

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-04-04, 16:55
wey97 wey97 is offline
Registered User
 
Join Date: Mar 2003
Posts: 130
Question How to find a record by number

What would be the best way in SQL to query a table to return a single record by number?

Let's say I want record number 49 and my primary key is a text field so a numeric key is out of the question.
Reply With Quote
  #2 (permalink)  
Old 05-04-04, 17:02
wey97 wey97 is offline
Registered User
 
Join Date: Mar 2003
Posts: 130
I think I figured it out unless anyone has a better way.

SELECT TOP 1 * FROM (SELECT TOP 49 * FROM [Table] ORDER BY [Key] ASC) AS Whatever ORDER BY [Key] DESC

Last edited by wey97; 05-04-04 at 18:14.
Reply With Quote
  #3 (permalink)  
Old 05-04-04, 18:16
wey97 wey97 is offline
Registered User
 
Join Date: Mar 2003
Posts: 130
That looks like a bad idea. If you pass any number bigger than the max number of records in the table, you always get the last record.

any ideas?
Reply With Quote
  #4 (permalink)  
Old 05-04-04, 20:36
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
which row did you want, the 49th? by which column?

and yes, if there are fewer than 49 rows, it would seem appropriate (at least, it seems so to me) to choose the last one

what database are you using? this forum is for non-specific (i.e. standard) sql

you may want to consider the Access or SQL Server forums, if TOP works in your database
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On