例子,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)。