Row Count

  • Is it Possible to get total count and Paginated Records using cte only with out subquery?

    Example:-

    with cte as (select *,Row_number() over(order by id) from TableName)

    select *,(select count (*) from cte) from cte where rownumber between some integers

    I need other better queries than this.

  • with cte as (select *,Row_number() over(order by id) as rownumber,

    count(*) over() as total from TableName)

    select * from cte where rownumber between some integers

    ____________________________________________________

    Deja View - The strange feeling that somewhere, sometime you've optimised this query before

    How to get the best help on a forum

    http://www.sqlservercentral.com/articles/Best+Practices/61537
  • hi, U sent

    with cte as (select *,Row_number() over(order by id) as rownumber,

    count(*) over() as total from TableName)

    select * from cte where rownumber between some integers

    ok its good,but for every row of cte,count wil b repeated.

    Do u think its better than what i have sent? If so can u pls explain

    Thanks in advance:-)

  • Suggest you try both approaches, in my experience "count(*) over()" doesn't perform particularly well.

    ____________________________________________________

    Deja View - The strange feeling that somewhere, sometime you've optimised this query before

    How to get the best help on a forum

    http://www.sqlservercentral.com/articles/Best+Practices/61537
  • Hi,

    I saw in Profiler,ur query wil perform poorly than my query.Urs wil take

    twice as much as reads and cpu and duration of my query.

    Any way thanks for replying.:-)

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

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