我编写了一个查询,需要为每条记录随机匹配五个可能值之一与大约 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
最佳答案
这是由于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 ...
关于sql - 为每条记录分配随机值,而不仅仅是整个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17404534/