Return every nth record from resultset

  • I have a query that returns 2590 records as the resultset. I was wondering if there was a way to return every nth record in the resultset to make it easier for the client to navigate the data. For example, I would like to return every 20th record from the resultset. Is there a quick and/or easy way to do this?

  • In SQL2005 with 'rownumber' introduced, you'll be able to do it without even thinking.

    However, in SQL2000 you would need to make a rownumber function for yourself.

    Look at http://support.microsoft.com/default.aspx?scid=KB;EN-US;q186133, and see how you can adapt this type of thing to your needs.

    Rob

    Rob Farley
    LobsterPot Solutions & Adelaide SQL Server User Group
    Company: http://www.lobsterpot.com.au
    Blog: http://blogs.lobsterpot.com.au

  • Try this in the Northwind database to select the every 5th record from products

    DECLARE @tbl TABLE (RowNum INT IDENTITY(1,1), ProductName VARCHAR(40))

    INSERT @tbl (ProductName)

    SELECT ProductName FROM dbo.Products

    SELECT * from @tbl

    WHERE RowNum % 5 = 0

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply