Share via

Help pelase - self join !

KubirSingh 21 Reputation points
2022-03-30T06:41:53.677+00:00

Hi,
This is the sample table and four sample records

Create table #Employee_Cleanup
( EMp id int,
TypeOfRecord varchar(10),
name varchar(100),
address varchar(100),
email varchar(100),
phonenumber varchar(10)
)

insert into #Employee_Cleanup values (200,'new', 'WillSmith', '','','0000000')
insert into #Employee_Cleanup values (200,'old', 'WillSmith', 'philly','Hit1234@Stuff .com','43555332')

insert into #Employee_Cleanup values (100,'new', 'ChrisRock', '','','65321332')
insert into #Employee_Cleanup values (100,'old', 'ChrisRock', 'southcarolina','fun1234@Stuff .com','12321332')

Now, my requirment is to update the 'New' TypeOfRecord record with the 'old' TypeOfrecord values, so that all the blank values will be updated with values
I am finding diffuculties during self -join - I am getting duplicates. Experts, Can you lease help !

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.


Answer accepted by question author

Bert Zhou-msft 3,521 Reputation points
2022-03-30T08:39:03.363+00:00

Hi,@KubirSingh
Welcome to Microsoft T-SQL Q&A Forum!

Please check this:

Create table #Employee_Cleanup  
    (   
    EMp_id int,  
    TypeOfRecord varchar(10),  
    name varchar(100),  
    address varchar(100),  
    email varchar(100),  
    phonenumber varchar(10)  
    )  
      
    insert into #Employee_Cleanup values (200,'new', 'WillSmith', '','','0000000')  
    insert into #Employee_Cleanup values (200,'old', 'WillSmith', 'philly','******@gmail.com','43555332')  
      
    insert into #Employee_Cleanup values (100,'new', 'ChrisRock', '','','65321332')  
    insert into #Employee_Cleanup values (100,'old', 'ChrisRock', 'southcarolina','******@gmail.com','12321332')  
      
    select * from #Employee_Cleanup  
      
    update new  
    set new.address=old.address,new.email=old.email  
    from #Employee_Cleanup new inner join #Employee_Cleanup old  
    on old.EMp_id=new.EMp_id  
    where new.TypeOfRecord='new'and old.TypeOfRecord='old'  
      
    select * from #Employee_Cleanup  

Here is the result:
188332-image.png

Best regards,
Bert Zhou


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. KubirSingh 21 Reputation points
    2022-03-30T12:40:18.787+00:00

    thanks much buddy!

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.