Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2011
    Posts
    7

    Question Unanswered: to select nth row

    Hi experts,

    How to select a Nth row in a table using MS sql server2005.

    EX:

    I want to select 59th row from a table of 2000 records.

  2. #2
    Join Date
    Apr 2008
    Location
    Iasi, Romania
    Posts
    561
    Provided Answers: 2
    WITH q AS (
    SELECT
    ROW_NUMBER() OVER (ORDER BY p.Name ) AS rownr,
    p.Name
    FROM Product p
    )q SELECT * FROM q WHERE q.rownr = 59
    Florin Aparaschivei
    DB2 9.7, 10.5 on Windows
    Iasi, Romania

  3. #3
    Join Date
    Sep 2010
    Posts
    153

    not working :(

    it wud be better ifnyou can explain with a table and its fields.


    thanks

  4. #4
    Join Date
    Apr 2008
    Location
    Iasi, Romania
    Posts
    561
    Provided Answers: 2
    Product it's a table of mine and Name it's a table column.
    Florin Aparaschivei
    DB2 9.7, 10.5 on Windows
    Iasi, Romania

  5. #5
    Join Date
    Sep 2010
    Posts
    153

    Smile how come it's not working for me?:O

    it is not working
    Tell me how to do it?


    Thanks

  6. #6
    Join Date
    Apr 2008
    Location
    Iasi, Romania
    Posts
    561
    Provided Answers: 2
    What it is not working? Post your SQL to see.
    Florin Aparaschivei
    DB2 9.7, 10.5 on Windows
    Iasi, Romania

  7. #7
    Join Date
    Sep 2010
    Posts
    153

    ok

    I have a table named as customer_details and tehre are tqo rows of records. Suppose if i want to see row number 2, then write me a query.

    i tried this query as mentioned by you, is it wrong?

    WITH q AS (
    SELECT
    ROW_NUMBER() OVER (ORDER BY p.customer_code ) AS rownr,
    p.customer_code
    FROM customer_details p
    )q SELECT * FROM q WHERE q.rownr = 2



    customer_details - table name

    customer_code - field name

    thanks

  8. #8
    Join Date
    Apr 2008
    Location
    Iasi, Romania
    Posts
    561
    Provided Answers: 2
    Sorry, there was an additional q in my statement:

    WITH q AS (
    SELECT
    ROW_NUMBER() OVER (ORDER BY p.customer_code ) AS rownr,
    p.customer_code
    FROM customer_details p
    ) SELECT * FROM q WHERE q.rownr = 2

    PS: this one i've tested so it should work
    Florin Aparaschivei
    DB2 9.7, 10.5 on Windows
    Iasi, Romania

  9. #9
    Join Date
    Sep 2010
    Posts
    153

    Thumbs up Thanks a lot, it's working :)

    Thanks a lot buddy. It's working now. I thought of removing q. I could have tried by removing q.


    Thanks

Posting Permissions

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