每个分类最新几条的SQL实现

来源:互联网 发布:小米擦除数据 编辑:程序博客网 时间:2024/04/26 21:52

分类统计的时候,我们经常会碰到这样的需求,每个类按照一定顺序,取几条数据,然后在一起显示。这个问题的解决方法,我们通过搜索引擎,可以找到很多种,但是不是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 

原创粉丝点击