因为专注,所以专业

我的Java Blog

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  21 随笔 :: 30 文章 :: 6 评论 :: 0 Trackbacks

分类统计的时候,我们经常会碰到这样的需求,每个类按照一定顺序,取几条数据,然后在一起显示。这个问题的解决方法,我们通过搜索引擎,可以找到很多种,但是不是SQL语句过于复杂,就是在数据量非常庞大的时候,性能就成了问题。

数据结构:

    create table tb(

        count int not null,

        type varchar(32) not null

    )

解决方案:

    select count, type from (select count, type, row_number() over(partition by type order type count desc) as rowindex from tb) t where rowindex <= 10 

posted on 2009-10-22 13:07 kevin.zhan 阅读(154) 评论(0)  编辑  收藏 所属分类: 数据库

只有注册用户登录后才能发表评论。


网站导航: