如何在Oracle中过去2年从表中选择10行

时间:2016-08-02 22:29:25

标签: sql oracle

我有一张桌子,过去4年里有近900万行 我需要制作一份接近过去2年的10万个样本的清单。 在这样做的同时,我可以从2014年7月开始每月选择X行。

<?php
mail('mymail@gmail.com', 'the subject', 'the body');
?>

由于

2 个答案:

答案 0 :(得分:0)

如果表tb1有一个列dt(我希望它不被称为DATE,那么它是一个保留关键字并将其用作列名称很可能会导致错误),如果你需要,比如100个纯随机行从过去24个月的每一个月开始,你都可以做到这样的事情。然后,如果需要,可以将结果连接到其他表。我假设tb1中的其他列(或连接所需的列)是col1,col2。

select col1, col2, dt
from (
        select col1, col2, dt, 
               row_number() over ( partition by trunc(dt, 'mm') 
                                   order by dbms_random.value() 
                                 ) as rn
        from   tb1
        where  dt between add_months(trunc(sysdate), -24) and sysdate
     )
where rn <= 100
;

答案 1 :(得分:0)

不确定您的SQL示例如何与您要求的英语相关... 但你可以尝试这样的事情:

with q1 as ( select trunc(Date, 'month') mth, 
                    min(Date) start_dt, 
                    max(Date) end_dt
             from table1
             where date ...
             group by trunc(Date, 'month')),
     q2 as (select table1.*, 
                   row_number() over (partition by trunc(Date,'month') 
                                      order by [some random column]) seq
       where Date ... )
select q2.*
from q1 
join q2 
  on q2.Date between q1.start_dt and q1.end_dt 
     and q2.seq <= [x]
相关问题