简体   繁体   中英

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:

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.

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. If that is your requirement, above is the query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM