[英]Oracle SQL add uniform random numbers to column
I want to create a new column based on an existing column plus some uniform random numbers.我想根据现有列加上一些统一的随机数创建一个新列。
Data数据
-- borrowed from https://stackoverflow.com/q/7745609/808921
CREATE TABLE IF NOT EXISTS `docs` (
`id` int(6) unsigned NOT NULL,
`rev` int(3) unsigned NOT NULL,
`content` float unsigned NOT NULL,
PRIMARY KEY (`id`,`rev`)
) DEFAULT CHARSET=utf8;
INSERT INTO `docs` (`id`, `rev`, `content`) VALUES
('1', '1', '1.24546'),
('2', '1', '1.245546546'),
('1', '2', '1.25654546'),
('1', '3', '1.2421323546');
Based on the OracleSQL documentation here , I tried:根据此处的 OracleSQL 文档,我尝试了:
SELECT id, rev, content,
content + DBMS_RANDOM.VALUE AS content2
FROM docs
There obviously is no "expected output" here, given the randomness, but I hope the schema + code are sufficiently clear to demonstrate what I am trying to achieve考虑到随机性,这里显然没有“预期输出”,但我希望架构+代码足够清晰,以展示我想要实现的目标
Try this尝试这个
SELECT id, rev, content,
content + DBMS_RANDOM.VALUE(1,10) AS content2
FROM docs
You have to use the start value and end value as the parameters in the dbms_random function.您必须使用起始值和结束值作为 dbms_random function 中的参数。
SELECT id, rev, content,
content + DBMS_RANDOM.VALUE(1, 20) AS content2 -- 1 - Start value, 20 - End value
FROM docs;
But not sure why you wanted to select random numbers and add it to your original value.但不知道为什么你想要 select 随机数并将其添加到原始值。 If that is your requirement, above is the query如果这是您的要求,上面是查询
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.