How to retrieve record from a specific row for tables that do not have an ID

Taking the example of the Northwind database, the table Customer has a primary key that is non-numeric. So in order to find the row at position 20 (ordered by CustomerID), we will need to do some workarounds to achieve the same. Let us see how we can do this easily by using the
ROW_NUMBER()

Query : To find the record at a particular row

USE NORTHWIND
GO
SELECT *
FROM ( SELECT ROW_NUMBER() OVER(ORDER BY CustomerID) AS cid, CustomerID, CompanyName, ContactName, ContactTitle
FROM Customers) C
WHERE cid = 20

1 comment:

  1. Also see what you can do with row_number() function

    http://beyondrelational.com/blogs/madhivanan/archive/2007/08/27/multipurpose-row-number-function.aspx

    Madhivanan
    http://beyondrelational.com/blogs/madhivanan

    ReplyDelete