如何使用 Oracle 中某个范围内的随机值更新表列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5793558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 23:28:50  来源:igfitidea点击:

How to update a table column with random values within a range in Oracle?

oraclerandom

提问by venky80

I have a table with 3 columns: - PRIMARY_KEY- AMOUNTS- RAND_AMOUNTS(all nulls)

我有一个包含 3 列的表: - PRIMARY_KEY- AMOUNTS- RAND_AMOUNTS(全为空)

I want the RAND_AMOUNTScolumn to be populated with following formula:

我希望RAND_AMOUNTS使用以下公式填充该列:

AMOUNT*(0-100 random value)/100

So for example if the amount row let us suppose is 10 and the random value generated for the row is 10 then RAND_AMOUNTshould be 10 * 10 / 100 = $1

因此,例如,如果金额行让我们假设是 10 并且为该行生成的随机值是 10 那么RAND_AMOUNT应该是10 * 10 / 100 = $1

回答by a_horse_with_no_name

dbms_random()is what you are looking for:

dbms_random()是您要查找的内容:

UPDATE the_table
   SET rand_amount = (amount * dbms_random.value(0, 100) ) / 100
WHERE amount IS NOT NULL

回答by HAL 9000

update yourtable set rand_amounts = dbms_random.value * amount;

回答by cagcowboy

Using:

使用:

dbms_random.value(lower-value-limit, upper-value-limit)

should do what you're looking for.

应该做你正在寻找的。