top 5th to 10 th record of a table

  • please send the ways of writing this like top or row_number.

    Malleswarareddy
    I.T.Analyst
    MCITP(70-451)

  • Since you know that this can be done using ROW_NUMBER(), try some code yourself and if you are stuck, then come back with proper test data and the script with which you are stuck. We can help you then.

    With this limited information i am sure not many will even bother to help you out.


    Kingston Dhasian

    How to post data/code on a forum to get the best help - Jeff Moden
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • I know that but some one of friend told that its take more time to execute.that why i posted if there is another way which executing faster than using row_number

    Malleswarareddy
    I.T.Analyst
    MCITP(70-451)

  • You can use this method:

    WITH Reservation AS

    (

    SELECT ResId,GuestName,

    ROW_NUMBER() OVER (ORDER BY ResId) AS 'RowNumber'

    FROM ResInfmas

    )

    SELECT *

    FROM Reservation

    WHERE RowNumber BETWEEN 5 AND 10

    ---
    Thinking is the hardest work there is, which is the probable reason so few engage in it.

    Sunitha
    😎

  • malleswarareddy_m (7/22/2010)


    I know that but some one of friend told that its take more time to execute.that why i posted if there is another way which executing faster than using row_number

    We cannot say for certain that some method is better than the other for your problem. It is certainly going to require an ORDERING whatever query you write and that will be the one aspect taking a lot of time. One possible solution you might consider is to add a Clustered Index on the ORDER BY Column( if you already don't have one on the table ). This will keep the data in Ordered manner and explicit ordering will not be necessary, which will in turn improve the performance.


    Kingston Dhasian

    How to post data/code on a forum to get the best help - Jeff Moden
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • You've been asked to provide some DDL and sample data. Since all we have is a generic problem, here is a generic solution.

    ;WITH CTE AS

    (

    SELECT *,

    RN = ROW_NUMBER() OVER (PARTITION BY UnknownColumns ORDER BY MoreUnknownColumns)

    FROM Unknown.TableName

    )

    SELECT *

    FROM CTE

    WHERE RN BETWEEN 5 AND 10

    Edit: Whoops! I now see that sunitkrishna has already posted an identical solution.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • malleswarareddy_m (7/22/2010)


    I know that but some one of friend told that its take more time to execute.that why i posted if there is another way which executing faster than using row_number

    If you want something else than using row_number you can try select result of you query into temp table adding indentity column, then select from it. You may consider adding clustered index on it as well. something like:

    select identity(int,1,1) as rid, * into #res

    from ( whole your current query without row_number) q

    order by [whetever columns]

    create unique clustered index ix_#res on #res(rid)

    Then select from #res...

    The above method may be faster in some cases - test it

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • malleswarareddy_m (7/22/2010)


    I know that but some one of friend told that its take more time to execute.that why i posted if there is another way which executing faster than using row_number

    I don't think you will find anything that is faster. However you do it you're going to need a method to order your records and ROW_NUMBER is just as fast as any other.

    └> bt



    Forum Etiquette: How to post data/code on a forum to get the best help[/url]

Viewing 8 posts - 1 through 7 (of 7 total)

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