具有行数oracle的分区

时间:2015-07-15 06:16:46

标签: sql oracle

我在Oracle DB中有一个视图,它看起来如下:

<IfModule mod_php5.c>
    #Session timeout
    php_value session.cookie_lifetime 1800
    php_value session.gc_maxlifetime 1800
</IfModule>

这个想法是:如果TYPE是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 然后返回2行(随机),如果TYPE是'S'则返回3行(随机)。
示例:

'L'

1 个答案:

答案 0 :(得分:0)

你应该告诉oracle如何获得3行或2行。一个想法是制作一行:

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 );

您可以按照分析功能中的任何内容订购。例如,您可以按dbms_random.random()订购以进行随机选择。

如果您的列numrows是正确的,并且这是您想要获得的行数,则where子句更简单:

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;