oracle或mysql获取分组后每组的前三条数据

来源:互联网 发布:iphone导入mac无照片 编辑:程序博客网 时间:2024/05/06 16:54

mysql :

select a.* from(select t1.*,(select count(*)+1 from 表 where 分组字段=t1.分组字段 and 排序字段<t1.排序字段) as group_idfrom 表 t1) awhere a.group_id<=3

oracle:

SELECT t.*            FROM (SELECT ROW_NUMBER() OVER(PARTITION BY 分组字段 ORDER BY 排序字段 DESC) rn,                  b.*                  FROM 表 b) t           WHERE t.rn <= 3  ;


0 0