【问题标题】:Is this possible to repeat this code N times?这可以重复此代码 N 次吗?
【发布时间】:2019-03-28 17:42:37
【问题描述】:

我有一个 ABC 表,它有 10M+ 行。它包含 customer_id、email、score。我创建了一个随机选择 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次得到不同的结果...谢谢!

【问题讨论】:

标签: sql oracle


【解决方案1】:

尝试以下查询 - 这是您的查询,但为了测试目的稍作修改(它不使用 ABC 表,并且使用 CONNECT BY LEVEL &lt;= 10 子句仅生成 10 行 - 但您可以将 10 替换为 1000)

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 次,而不是每次测试的随机数据。

【讨论】:

  • 谢谢!但是我们能从这个 ABC 表中生成随机数吗?我需要将输出与其他表的其他输出进行比较。
猜你喜欢
  • 1970-01-01
  • 2022-01-13
  • 1970-01-01
  • 2018-02-17
  • 2023-03-31
  • 2022-06-14
  • 1970-01-01
  • 1970-01-01
  • 2020-07-02
相关资源
最近更新 更多