看看我是否可以根据总计数生成一个非重复随机整数-例如下面的代码-
with all_val as (
select 1 as ID, 'A' as val from dual union all
select 2 as ID, 'A' as val from dual union all
select 3 as ID, 'A' as val from dual union all
select 4 as ID, 'B' as val from dual union all
select 5 as ID, 'B' as val from dual union all
select 6 as ID, 'B' as val from dual union all
select 7 as ID, 'A' as val from dual union all
select 8 as ID, 'A' as val from dual union all
select 9 as ID, 'A' as val from dual)
select A.*,
(DBMS_RANDOM.VALUE( 0, X?? )) as RND from (
select B.* from all_val B
where VAL <> 'B') A;
ID的1-3和7-9将被选中,但生成的随机数只能是1-6。
希望这有意义。
Tia
1条答案
按热度按时间blmhpbnm1#
如果你想让随机数的范围从1到查询返回的行数,你可以通过传递
count(*) over ()
作为dbms_random.value
的上限来得到这个值: