根据条件在Oracle表中随机记录

急救人员

我有一个带有以下各列的Oracle表

表结构

表结构

在查询中,我需要返回所有CPER> = 40的记录,这是微不足道的。但是,除了CPER> = 40外,我还需要为每个CPID列出5条随机记录。我附上了记录的样本清单。但是,在我的表中,我有大约50,000条记录。谢谢您的帮助。

约翰·HC

Oracle解决方案:

with CTE as
(
select t1.*, 
       row_number() over(order by DBMS_RANDOM.VALUE) as rn -- random order assigned
from MyTable t1
where CPID <40
)
select *
from CTE
where rn <=5 -- pick 5 at random

union all
select t2.*, null
from my_table t2
where CPID >= 40

SQL Server:

with CTE as
(
select t1.*, 
       row_number() over(order by newid()) as rn -- random order assigned
from MyTable t1
where CPID <40
)
select *
from CTE
where rn <=5 -- pick 5 at random

union all
select t2.*, null
from my_table t2
where CPID >= 40

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章