Select a Subset of rows and count of all rows in one statement in case of paging

  • Hi all

    it is possible ,we can get a subset of rows according paging condition and total rows in one statement like

    Select * From

    (

    Select ID,Name,row_number() over(order by ID) as Row from Table1

    ) as VT Where Row between 50 and 60

    This query run but return only 10 records filter according paging condition.

    but Without paging condition we get total 100 record.

    we want get total record count in this statement.

    is it possible

    thanks in advance.

  • Hi

    The trick is to use inline query. Notice that I moved your SQL to CTE to allow for complex, yet coordinated, where clause...

    ;WITH MySampleCTE AS

    (

    Select

    row_number() over(order by name) as Row,

    *

    from

    sys.objects

    )

    Select

    *,

    (SELECT COUNT(*) FROM MySampleCTE) as TotalRecords

    From

    MySampleCTE as VT

    Where

    Row between 50 and 60

  • Thanks For Reply

    Your Solution is right. but according performance issue it is not good.

    if we use 4 - 5 table in join and select a range from 2000 to 3000 row number then sub query use in this statement is execute every time.it mean 1000 times. so it is not good according performance.

    please give me new ideas for solve this problems

  • You've posted into 'Sql Server 7,2000' forum are you not on a later release ?

    In guessing so as you have used row_number().

    See this article (and the following discussion for advice )

    http://qa.sqlservercentral.com/articles/T-SQL/66030/



    Clear Sky SQL
    My Blog[/url]

  • Thank

    This can help me

  • Great solution!!!

Viewing 6 posts - 1 through 5 (of 5 total)

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