对于列表中的每个项目,oracle pl / sql

时间:2017-04-06 18:34:03

标签: sql oracle plsql

我有一个问题:

select sq1.* from (
    select * from table1
    where type = '1'
    order by name
) sq1
where rownum <= 10;

但我必须为类型2,3,a,b,c运行此列表。有没有办法做这样的事情?

my @list = ('1', '2', '3', 'a', 'b', 'c');
for each my $item in @list {
    select sq1.* from (
        select * from table1
        where type = $item
        order by name
    ) sq1
    where rownum <= 10;
}

(是的,我的代码在perl中。这是我所知道的全部。)

1 个答案:

答案 0 :(得分:1)

SELECT sq1.* FROM (
    SELECT 
        tbl.*, 
        ROW_NUMBER() OVER (PARTITION BY type ORDER BY name) AS rnk 
    FROM table1 tbl
    WHERE type IN ('1', '2', '3', 'a', 'b', 'c')

) sq1
WHERE rnk <= 10;