Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2003
    Posts
    130

    Question Unanswered: 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.

  2. #2
    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 19:14.

  3. #3
    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?

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    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
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •