示例:select EMPLOYEENAME from EMPLOYEE where state in ('NY','CA','TX');
有没有办法从 where 子句中的每个值返回 10 行行,即每个州的 10 个名称。
编辑:我正在使用 Oracle DB。
SELECT a.* FROM
(
select EMPLOYEENAME ,
ROW_NUMBER() OVER(PARTITION BY state ORDER BY EMPLOYEENAME) as rn
from EMPLOYEE where state in ('NY','CA','TX')
)a WHERE rn <=10;
select *
from
(
select foo,bar,
row_number() over(partition by baz) rn
from table
group by foo,bar
)
where rn <= 10
使用分析功能,例如row_number
允许基于某些标准进行分区。这基本上会覆盖rownum
oracle 分配的默认值。
如果您担心并发性,请记住读取将基于事务锁定并且不会阻止插入/更新。