您可以使用以下命令执行此操作left outer join
:
select *
from (select u.user_key,
count(*) over () as numrecs
from users u left outer join
user_validation uv
on u.user_key = uv.user_key and
uv.validation_state <> 'expired'
where u.active_flag = 'Y' and uv.user_key is null
) t
where rownum <= numrecs * 0.05
您正在使用示例子句。目前尚不清楚您是否只想要您选择的 5% 中的不匹配数据,或者您是否想要 5% 的不匹配数据。这是后者。
编辑:根据作者的评论添加示例:
select user_key from (
select u.user_key, row_number() over (order by dbms_random.value) as randval
from users u
left outer join user_validation uv
on u.user_key = uv.user_key
and uv.validation_state <> 'expired'
where u.active_flag = 'Y'
and uv.user_key is null
) myrandomjoin where randval <=100;