在Oracle查询的每一行中生成随机数 [英] Generating Random Number In Each Row In Oracle Query

查看:72
本文介绍了在Oracle查询的每一行中生成随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想选择一个表的所有行,然后选择一个1到9之间的随机数:

I want to select all rows of a table followed by a random number between 1 to 9:

select t.*, (select dbms_random.value(1,9) num from dual) as RandomNumber
from myTable t

但是行与行之间的随机数相同,仅与每次查询运行不同.如何在同一执行中使行与行之间的数字不同?

But the random number is the same from row to row, only different from each run of the query. How do I make the number different from row to row in the same execution?

推荐答案

类似?

select t.*, round(dbms_random.value() * 8) + 1 from foo t;

大卫指出,这会导致1和9的分布不均匀.

David has pointed out this gives uneven distribution for 1 and 9.

正如他指出的那样,以下内容可以提供更好的分布:

As he points out, the following gives a better distribution:

select t.*, floor(dbms_random.value(1, 10)) from foo t;

这篇关于在Oracle查询的每一行中生成随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆