可以将此代码重复N次吗?

时间:2019-03-28 17:42:37

标签: sql oracle

我有一个表ABC,它具有10M +行。它包含customer_id,电子邮件,分数。我创建了一个查询,该查询随机选择1,000个数字并按组对它们进行计数。此查询的输出如下。

RANDOM_GROUP    FREQ
1            71
2            45
3            35
4            45
5            53
6            40
7            65
8            54
9            68
10           59
11           465

这是我的查询

select random_group, count(random_group) as freq
from
      (select case when rand_num >= 0 and rand_num<=0.053 then 1
                  when rand_num > 0.053 and rand_num <= 0.097 then 2
                  when rand_num > 0.097 and rand_num <= 0.142 then 3
                  when rand_num > 0.142 and rand_num <= 0.189 then 4
                  when rand_num > 0.189 and rand_num <= 0.234 then 5
                  when rand_num > 0.234 and rand_num <= 0.281 then 6
                  when rand_num > 0.281 and rand_num <= 0.341 then 7
                  when rand_num > 0.341 and rand_num <= 0.399 then 8
                  when rand_num > 0.399 and rand_num <= 0.458 then 9
                  when rand_num > 0.458 and rand_num <= 0.515 then 10
                  when rand_num > 0.515 and rand_num <= 1.000 then 11 end 
                  random_group
      from
            (SELECT dbms_random.value(0,1) AS rand_num, 
                    rownum as at_row_num
             FROM ABC                   
             WHERE rownum <= 1000))
group by random_group
order by random_group
; 

是否可以重复此查询N次,以便每个输出生成不同的数字计数?我应该使用循环吗?我不想手动运行N次以获得不同的结果...谢谢!

1 个答案:

答案 0 :(得分:0)

请尝试以下查询-这是您的查询,但出于测试目的进行了一些修改(它不使用ABC表,并且使用CONNECT BY LEVEL <= 10子句仅生成10行-但您可以将1000替换为10)

WITH My_Query As (
        select random_group, count(random_group) as freq
        from
              (select case when rand_num >= 0 and rand_num<=0.053 then 1
                          when rand_num > 0.053 and rand_num <= 0.097 then 2
                          when rand_num > 0.097 and rand_num <= 0.142 then 3
                          when rand_num > 0.142 and rand_num <= 0.189 then 4
                          when rand_num > 0.189 and rand_num <= 0.234 then 5
                          when rand_num > 0.234 and rand_num <= 0.281 then 6
                          when rand_num > 0.281 and rand_num <= 0.341 then 7
                          when rand_num > 0.341 and rand_num <= 0.399 then 8
                          when rand_num > 0.399 and rand_num <= 0.458 then 9
                          when rand_num > 0.458 and rand_num <= 0.515 then 10
                          when rand_num > 0.515 and rand_num <= 1.000 then 11 end 
                          random_group
              from
                    (SELECT dbms_random.value(0,1) AS rand_num, 
                            rownum as at_row_num
                     FROM dual
                     CONNECT BY LEVEL <= 10))
        group by random_group
        order by random_group
)
SELECT * FROM (
        SELECT level as test_number FROM dual
        CONNECT BY LEVEL <= 100
) row_generator,
LATERAL (
    SELECT * FROM My_Query WHERE row_generator.test_number = row_generator.test_number
) x
; 

顺便说一句:WHERE row_generator.test_number = row_generator.test_number子句似乎是多余的,但是如果不这样做,您将得到错误的结果-相同的记录重复100次,而不是每次测试都重复随机数据。