1

示例:select EMPLOYEENAME from EMPLOYEE where state in ('NY','CA','TX');

有没有办法从 where 子句中的每个值返回 10 行行,即每个州的 10 个名称。

编辑:我正在使用 Oracle DB。

4

2 回答 2

7
 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;
于 2012-12-31T17:58:38.860 回答
2
select *  
from  
(  
    select foo,bar,  
    row_number() over(partition by baz)  rn     
    from table
    group by foo,bar
)  
where rn <= 10

使用分析功能,例如row_number允许基于某些标准进行分区。这基本上会覆盖rownumoracle 分配的默认值。

如果您担心并发性,请记住读取将基于事务锁定并且不会阻止插入/更新。

于 2012-12-31T17:55:41.030 回答