如何在分页SQL Server查询时获取结果计数

时间:2012-11-30 12:25:58

标签: sql pagination sql-server-2008-r2 rowcount

为了显示总页数计数,我还需要在以下查询中检索结果总数。我怎么能这样做?

谢谢

select 
    AvgLevel, TotalCount, PokemonId, Type1, Speed, MonsterTotalStats 
from 
   (select 
       row_number() over (order by tblPokedex.PokemonId asc) rowNumber,
       AvgLevel, TotalCount, tblPokedex.PokemonId, 
       Type1, tblPokedex.Speed, MonsterTotalStats
    from 
       tblPokemonStats, tblAvailablePokemons, tblPokedex 
    left join 
       tblUsersPokemons on tblPokedex.PokemonId = tblUsersPokemons.PokemonId  
    where 
       tblPokemonStats.PokemonId = tblPokedex.PokemonId 
       and tblPokedex.Class = 'emissary' 
    group by 
       tblPokedex.PokemonId, tblPokedex.Type1, tblPokedex.Speed, 
       tblPokemonStats.AvgLevel, tblPokemonStats.TotalCount, MonsterTotalStats 
   ) result 
where 
    result.rowNumber > 0 and result.rowNumber < 101

2 个答案:

答案 0 :(得分:2)

只需添加列Count(*) over()

..... 
select row_number() 
over (order by tblPokedex.PokemonId asc) rowNumber,
AvgLevel,
Count(*) over() as TotalCount,
.......

参见示例

select 
AvgLevel,TotalCount,PokemonId,Type1,Speed,MonsterTotalStats, TOTALRECORDCOUNT
from (
select row_number() 
over (order by tblPokedex.PokemonId asc) rowNumber,AvgLevel,TotalCount,tblPokedex.PokemonId,Type1,tblPokedex.Speed,MonsterTotalStats,
count(*) over() as TOTALRECORDCOUNT
from tblPokemonStats,tblAvailablePokemons,tblPokedex left join tblUsersPokemons on tblPokedex.PokemonId=tblUsersPokemons.PokemonId  
where tblPokemonStats.PokemonId=tblPokedex.PokemonId and tblPokedex.Class='emissary' 
group by tblPokedex.PokemonId,tblPokedex.Type1,tblPokedex.Speed,tblPokemonStats.AvgLevel,tblPokemonStats.TotalCount,MonsterTotalStats 
)  
result where result.rowNumber>0 and result.rowNumber<101

答案 1 :(得分:1)

您可以使用:

SELECT @@ROWCOUNT

查看here