I want to update a table to have the data shuffled around. Let's say I have a table with 5 columns. column 1 is the primary key so that'll remain unchanged. columns 2 and 3 will be shuffled to different rows. columns 4 and 5 will also be shuffled but as pairs (they must be moved together).
How would I write the script for this? The primary key is a random unique identifier so it doesn't correlate with the row number.
Before:
| c1 | c2 | c3 | c4 | c5 |
|---|---|---|---|---|
| 10 | 1 | 2 | 3 | 4 |
| 20 | 5 | 6 | 7 | 8 |
| 30 | 9 | 10 | 11 | 12 |
| 40 | 13 | 14 | 15 | 16 |
After:
| c1 | c2 | c3 | c4 | c5 |
|---|---|---|---|---|
| 10 | 5 | 14 | 11 | 12 |
| 20 | 9 | 6 | 15 | 16 |
| 30 | 1 | 2 | 3 | 4 |
| 40 | 13 | 10 | 7 | 8 |
To provide an update on why I am doing this, this isn't a production database or anything. I'm shuffling the data around to anonymize the data for testing purposes, that's about it.
To provide additional details, say this is a people table. I might shuffle the address or phone number to anonymize the person.
If temporary tables are allowed:
You can use an update-with-join query:
DB<>Fiddle