Difference between revisions of "Oracle/Shuffle data"

From YavInWiki
Jump to navigation Jump to search
Line 19: Line 19:


'''Note: NULL values will be lost (will be set to the same random value).'''
'''Note: NULL values will be lost (will be set to the same random value).'''
Initial idea came from [http://asktom.oracle.com/pls/apex/f?p=100:11:0::NO::P11_QUESTION_ID:6382864029082 Ask Tom].

Revision as of 13:15, 5 May 2011

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).

Initial idea came from Ask Tom.