接触db2没多久,要做一个group by 后取count(*)前10条的功能 ,查了一下db2语法,翻页是靠rownumber() over() as rowno,在rowno between xx and xx做得,于是写了下面一条sql测试
select * from (select search_name as name,count(*) as count, rownumber() over() as rowno from yoyokm_activity_search where search_name !='' group by search_name order by count(*) desc ) as top
结果:
NAME COUNT ROWNO
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------- --------------------
世纪之村 45 1
公司 42 2
test 30 3
install 28 4
1 17 5
名字2 13 6
吃 9 7
名字3 8 8
。。。。
top.rowno按照count(*)排序,看起来一切很正常,在加上between,
select * from (select search_name as name,count(*) as count, rownumber() over() as rowno from yoyokm_activity_search where search_name !='' group by search_name order by count(*) desc ) as top where top.rowno between 1 and 25
结果
NAME COUNT ROWNO
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------- --------------------