SQL - Swap values in rows

Hi.  
What's the best way in SQL to swap values in rows?  

For example, I have these rows:
UID	HDR_ID	Code	Perc
---	------	----	----
14	280	26	40
15	280	24	50
16	280	1	10
67	482	26	10
68	482	17	20
69	482	7	20
84	482	24	10
85	482	2	20
91	482	1	20

Open in new window

and I need to swap the HDR_ID's.  So, I'd end up with 3 records for 482 and 6 records for 280.
I assume I can do it with a TEMP table, but is there a simpler way?
Thanks for your suggestions!
Rob RudloffIT Development SpecialistAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulfreelancerCommented:
Do you mean to UPDATE the original rows (the change is permanent)?

or do you just wish to display 482 when the data is 280 (and vice versa)?
0
Rob RudloffIT Development SpecialistAuthor Commented:
Yes, update them.
-- thanks.
0
PortletPaulfreelancerCommented:
for any suggestion involving an update, please make certain you can restore the data to the 'as is' state.

**MS SQL Server 2008 Schema Setup**:

    CREATE TABLE Table1
        ([UID] int, [HDR_ID] int, [Code] int, [Perc] int)
    ;
        
    INSERT INTO Table1
        ([UID], [HDR_ID], [Code], [Perc])
    VALUES
        (14, 280, 26, 40),
        (15, 280, 24, 50),
        (16, 280, 1, 10),
        (67, 482, 26, 10),
        (68, 482, 17, 20),
        (69, 482, 7, 20),
        (84, 482, 24, 10),
        (85, 482, 2, 20),
        (91, 482, 1, 20)
    ;
    
**Query 1**:

    ;WITH CTE AS (
          SELECT
                 UID
               , CASE WHEN HDR_ID = 280 then 482
                      WHEN HDR_ID = 482 then 280
                 END AS new_HDR_ID
               , HDR_ID
          FROM Table1
          WHERE HDR_ID IN (280,482)
        )
    UPDATE CTE
    SET HDR_ID = new_HDR_ID
    

**[Results][2]**:
**Query 2**:

    
    
    SELECT * FROM table1
    

**[Results][3]**:
    | UID | HDR_ID | Code | Perc |
    |-----|--------|------|------|
    |  14 |    482 |   26 |   40 |
    |  15 |    482 |   24 |   50 |
    |  16 |    482 |    1 |   10 |
    |  67 |    280 |   26 |   10 |
    |  68 |    280 |   17 |   20 |
    |  69 |    280 |    7 |   20 |
    |  84 |    280 |   24 |   10 |
    |  85 |    280 |    2 |   20 |
    |  91 |    280 |    1 |   20 |

  [1]: http://sqlfiddle.com/#!3/6d713/3

Open in new window

{+ edit} note a CTE is conceptually similar to a temp table, it's just a wee bit more temporary perhaps :)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Rob RudloffIT Development SpecialistAuthor Commented:
That will do it.  
I was able to try it out, and substituted the values with variables, like below.
No experience with CTE, but I see now that changes to the CTE table automatically cascade to the original table, so I don't have to do an "update" on Table1 -- just make the changes to CTE.
Thanks for your help!
CASE WHEN HDR_ID = val1 then val2
     WHEN HDR_ID = val2 then val1

Open in new window

0
PortletPaulfreelancerCommented:
>>"are CTE changes also changing the original records?"
Yes

Look back at my test case, I use [Table1] then derived a CTE of that, update the CTE

then I show all records in Table1 << that is why I included this
to demonstrate you are actually updating the underlying table
0
Rob RudloffIT Development SpecialistAuthor Commented:
Included my response as a 0-point solution, so that I will remember to read my comments regarding CTE.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.