title: Shuffle a column between rows
body: <p>How can I shuffle the contents of a large (1m to 5m record) table efficiently? The column is known to have unique values, but you can assume that all constraints are removed for the purposes of this. My headaches are primarily because I am updating the same column I am selecting from. My goal is to do this with PL/SQL so that I can take additional action programmatically such as logging or updating other tables.</p>
  
  <pre><code>**Original table:**
  +----+-----------+
  | id | fname     |
  +----+-----------+
  |  1 | mike      |
  |  2 | ricky     |
  |  3 | jane      |
  |  4 | august    |
  |  6 | dave      |
  |  9 | Jérôme    |
  +----+-----------+
  
  **Possible output:**
  +----+-----------+
  | id | fname     |
  +----+-----------+
  |  1 | dave      |
  |  2 | jane      |
  |  3 | mike      |
  |  4 | ricky     |
  |  6 | Jérôme    |
  |  9 | august    |
  +----+-----------+
  </code></pre>
  
  <p>My latest attempts have been to create a cursor that uses <code>over (order by dbms_random.value)</code> and to try to do a merge or update perhaps based on rownum. Perhaps I can get around the modifying self constraint by creating a temp table of sorts? I'm fairly confident Oracle has some fancy way to do this but I am limited in my SQL abilities to the basic CRUD commands.</p>
  
  <p>The full solution is here, based on Gordon's answer:</p>
  
  <pre><code>merge into t
  using (
  select t.id, t2.name
  from (select t.*, rownum as seqnum
        from t
       ) t join
       (select t.*, row_number() over (order by dbms_random.value) as seqnum
        from t
       ) t2
       on t.seqnum = t2.seqnum
  ) src
  on (t.id = src.id)
  when matched then update set t.name = src.name;
  </code></pre>