Sql 具有oracle行数的分区
我在Oracle DB中有一个视图,它如下所示:Sql 具有oracle行数的分区,sql,oracle,Sql,Oracle,我在Oracle DB中有一个视图,它如下所示: id | type | numrows ----|--------|---------- 1 | S | 2 2 | L | 3 3 | S | 2 4 | S | 2 5 | L | 3 6 | S | 2 7 | L
id | type | numrows
----|--------|----------
1 | S | 2
2 | L | 3
3 | S | 2
4 | S | 2
5 | L | 3
6 | S | 2
7 | L | 3
8 | S | 2
9 | L | 3
10 | L | 3
其思想是:如果类型为“S”,则随机返回2行,如果类型为“L”,则随机返回3行。
例如:
id | type | numrows
----|--------|----------
1 | S | 2
3 | S | 2
2 | L | 3
5 | L | 3
7 | L | 3
您应该告诉oracle如何获取3行或2行。IDEA是制作一行:
select id, type, numrows
from
(select
id,
type,
numrows,
row_number() over (partition by type order by type) rnk --fabricated
from table)
where
(type = 'S' and rnk <= 2 )
or
(type = 'L' and rnk <= 3 );
嗨,迭戈。你试过什么?
select id, type, numrows
from
(select
id,
type,
numrows,
row_number() over (partition by type order by dbms_random.random()) rnk --fabricated
from table)
where
rnk <= numrows;