简体   繁体   English

如何在Oracle中重复查询

[英]How to repeat a query in Oracle

I need to repeat this query 100 times. 我需要将此查询重复100次。 I think I should use loop function but didn't find any solutions on how to do it. 我认为我应该使用循环功能,但是没有找到解决方案。

select case when DBMS_RANDOM.value >= 0 and DBMS_RANDOM.value<=0.053 then 1
            when DBMS_RANDOM.value > 0.053 and DBMS_RANDOM.value <= 0.097 then 2
            when DBMS_RANDOM.value > 0.097 and DBMS_RANDOM.value <= 0.142 then 3
end random_groups
from temp_trt;

here is the sample data of temp_trt enter image description here 这是temp_trt的样本数据在这里输入图像描述

Each call of DBMS_RANDOM.value() returns a different value . 每次调用DBMS_RANDOM.value()返回一个不同的value Consequently there is no guarantee that any call will fall between any of your bounds. 因此, 不能保证任何通话都将落在您的任何界限之间。 In fact it's statistically unlikely. 实际上,从统计学上讲这不太可能。 Consequently most of the time you'll get a NULL returned, because you have defined no ELSE branch. 因此,在大多数情况下,您将返回NULL,因为您没有定义ELSE分支。

Here is an alternate solution which generates one hundred random values. 这是产生一百个随机值的替代解决方案。

with dr as (
    select DBMS_RANDOM.value val
    from dual
    connect by level <= 100 
)
select dr.val
      , case when dr.val >= 0 and dr.val<=0.053 then 1
            when dr.val > 0.053 and dr.val <= 0.097 then 2
            when dr.val > 0.097 and dr.val <= 0.142 then 3
            else 4 
end random_groups
from dr
;

Given the way your code defines the bounds of the branches, most of the random_groups will be 4 . 根据您的代码定义分支范围的方式,大多数random_groups将为4


It's not clear from your posted (toy?) code what the role of TEMP_TRT is, so I decided to ignore it. 从您发布的代码(玩具?)还不清楚,TEMP_TRT的作用是什么,因此我决定忽略它。 Please edit your question to add more detail if this makes you unhappy 如果您不满意,请编辑您的问题以添加更多详细信息

You can use row generation tecnique by select level ... connect by level >= 100 structure as 您可以通过select level ... connect by level >= 100使用行生成技术select level ... connect by level >= 100结构连接为

select level, rg.random_groups
 from
    ( select case when DBMS_RANDOM.value >= 0 and DBMS_RANDOM.value<=0.053 then 1
                when DBMS_RANDOM.value > 0.053 and DBMS_RANDOM.value <= 0.097 then 2
                when DBMS_RANDOM.value > 0.097 and DBMS_RANDOM.value <= 0.142 then 3
     end random_groups
    from dual ) rg
connect by level <= 100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM