2013-07-01 59 views
2

我写了一个查询,它需要将五个可能值中的一个与每个记录的随机约1500条记录匹配。我设法让它随机分配一个值,但分配的值对于每条记录都是相同的。有没有这样做,而不使用PL/SQL?请让我知道你的想法。查询是下面(数据库的Oracle 11g):为每个记录分配随机值,而不仅仅是整个查询

select 
ioi.ioi_mstc 
,ioi.ioi_seq2 
,max(decode(rn, (select round(dbms_random.value(1,5)) num from intuit.srs_ioi where rownum < 2), uddc)) 
from 
intuit.srs_ioi ioi 
,intuit.srs_cap cap 
    ,(select 
    sub.udd_code uddc 
    ,row_number() over(partition by sub.udd_udvc order by rownum) rn 
    from 
     (select * from 
     intuit.men_udd udd 
     where 
     udd.udd_udvc = 'PA_REJ_REAS' 
     order by dbms_random.value) sub 
    where rownum <= 5) rejReas 
where 
ioi.ioi_stuc = cap.cap_stuc 
and ioi.ioi_iodc = 'PAPERLESS' 
and cap.cap_ayrc = '2013/4' 
and cap.cap_idrc like '%R%' 
group by ioi.ioi_mstc 
,ioi.ioi_seq2 

回答

2

这是由于sub-query caching。考虑以下查询和返回的值:

SQL> with numbers as (
    2 select level as lvl 
    3  from dual 
    4 connect by level <= 10 
    5   ) 
    6 select lvl 
    7  , (select dbms_random.value(1,5) 
    8    from dual) as sq 
    9  , dbms_random.value(1,5) as nsq 
10 from numbers 
11   ; 

     LVL   SQ  NSQ 
---------- ---------- ---------- 
     1 2.56973281 2.86381746 
     2 2.56973281 3.54313541 
     3 2.56973281 1.71969631 
     4 2.56973281 3.71918833 
     5 2.56973281 3.10287264 
     6 2.56973281 3.9887797 
     7 2.56973281 2.6800834 
     8 2.56973281 3.57760938 
     9 2.56973281 2.47035426 
     10 2.56973281 3.77448435 

10 rows selected. 

SQL> 

该值正在被子查询缓存;只需将其删除。

select ioi.ioi_mstc 
    , ioi.ioi_seq2 
    , max(decode(rn, round(dbms_random.value(1,5)) , uddc)) 
    from ... 
+0

你能分享更多的细节,我将如何阻止这个值被缓存?提前致谢。 –

+0

我已经说得更清楚了@安迪。 – Ben

+0

完美,谢谢! –

相关问题