Oracle/Shuffle data

From YavInWiki
Revision as of 12:55, 5 May 2011 by Andy (talk | contribs)
Jump to navigation Jump to search

The following statement shuffles to content of columns (name and street):

-- shuffle the columns name and street (separately)
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) t2
    ON (t1.address_id = t2.address_id)
WHEN matched THEN
UPDATE
   SET t1.name = t2.name, t1.street = t2.street;

The individual random values are used so that both columns are shuffled separately (else they would stay together as a pair).

Note: NULL values will be lost (will be set to the same random value).