oracle分组取前3条记录

select bb.name, bb.id
  from (select aa.*,
               row_number() over(partition by aa.group_id order by aa.name) rn
          from (select bo.group_id, bc.name, bo.id
                  from table_a bc, table_b bo
                 where 1 = 1
                   and regexp_instr(bc.name, '测试') = 0
                   and bc.id = bo.ref_id
                 order by bo.id desc) aa) bb
 where bb.rn <= 3

   主要用到了regexp_instr和row_number() over(partition by A order by B)。

    如有错误请指出,谢谢。

    全文完

猜你喜欢

转载自53873039oycg.iteye.com/blog/2048267