如何使用 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
How to update a table column with random values within a range in Oracle?
提问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_AMOUNTS
column 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_AMOUNT
should 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.
应该做你正在寻找的。