简体   繁体   English

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

[英]How do I select 10 rows from a table for past 2 years in Oracle

I have a table that has close to 9 million+ rows from past 4 years I need to make a list close to 100 thousand samples from past 2 years. 我有一个表,过去4年中有近900万个行,我需要列出过去2年中接近10万个样本的列表。 While doing so I can select X rows from each month starting July 2014. 在这样做时,我可以从2014年7月开始的每个月中选择X行。

    select distinct tb1.field
    from table1 tb1
    join table2 tb2 
    on tb.field = tb2.field
    where tb1.Date between to_date('1-July-2014','DD-MON-YYYY') and     to_date('8-Aug-2016','DD-MON-YYYY') 
    order by tb1.field

Thanks 谢谢

If table tb1 has a column dt (I hope it's not called DATE, that is a reserved keyword and using it as a column name will most likely result in an error), and if you need, say, 100 purely random rows from each of the last 24 months, you could do something like this. 如果表tb1的列为dt(我希望它不被称为DATE,那是一个保留关键字,并将其用作列名很可能会导致错误),并且如果您需要从每个表中获取100条纯随机行在过去的24个月中,您可以执行以下操作。 Then you can join the result to other tables if you need to. 然后,您可以根据需要将结果连接到其他表。 I assume the other columns in tb1 (or the ones you need for the join) are col1, col2. 我假设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
;

Not sure how your SQL example is related to what you are asking for in English... But you can try something like this: 不确定您的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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM