Difference between revisions of "Oracle/Shuffle data"
Jump to navigation
Jump to search
(Created page with "The following statement(s) creates a copy of an existing table (address), randomly shuffles each (selected) column within the temporary table and puts them back into the original...") |
|||
| Line 1: | Line 1: | ||
The following statement(s) creates a copy of an existing table (address), randomly shuffles each (selected) column within the temporary table and puts them back into the original table. Afterwards the data within the original table (selected columns only) are randomly shuffled. The individual random min/max values are mandatory since without them all columns (here name and street) would be 'shifted' together (the pair would stay together). | The following statement(s) creates a copy of an existing table (address), randomly shuffles each (selected) column within the temporary table and puts them back into the original table. Afterwards the data within the original table (selected columns only) are randomly shuffled. The individual random min/max values are mandatory since without them all columns (here name and street) would be 'shifted' together (the pair would stay together). | ||
<source lang=" | <source lang="oracle11"> | ||
CREATE TABLE address_tmp as SELECT * FROM address; | CREATE TABLE address_tmp as SELECT * FROM address; | ||
MERGE INTO address t1 | MERGE INTO address t1 | ||
Revision as of 11:28, 5 May 2011
The following statement(s) creates a copy of an existing table (address), randomly shuffles each (selected) column within the temporary table and puts them back into the original table. Afterwards the data within the original table (selected columns only) are randomly shuffled. The individual random min/max values are mandatory since without them all columns (here name and street) would be 'shifted' together (the pair would stay together).
CREATE TABLE address_tmp as SELECT * FROM address;
MERGE INTO address t1
USING (SELECT address_id,
nvl(lead(name) over (order by dbms_random.value(1,1)),
first_value(name) over (order by dbms_random.value(1,1))) name,
nvl(lead(street) over (order by dbms_random.value(2,2)),
first_value(street) over (order by dbms_random.value(2,2))) street
FROM address_tmp) t2
ON (t1.address_id = t2.address_id)
WHEN matched THEN
UPDATE
SET t1.name = t2.name, t1.street = t2.street;
--drop table address_tmp;
If no temp table is used in the above example, data will (most likely) be lost since the select statement will work a changing table so that some information will be multiplied and some will be lost.