当前位置: 代码迷 >> DB2 >> 问个db2 groupby后取count(*)最多的前10条遇到的有关问题
  详细解决方案

问个db2 groupby后取count(*)最多的前10条遇到的有关问题

热度:10170   发布时间:2013-02-26 00:00:00.0
问个db2 groupby后取count(*)最多的前10条遇到的问题
接触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  
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------- --------------------
  相关解决方案