Query Join

RIAS
RIAS used Ask the Experts™
on
Hello,

Need a suggestion on how to use the join in the following example.

Thanks in advance.
Example5.xlsx
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
select t1.Ref, t2.Code from Table1 t1 left outer join Table2 t2 on t1.Ref = t2.Ref

Author

Commented:
Thanks but , how to update the column 'Code' based on the match?
what do you mean "update the column 'Code' based on the match"?
You want to set value of Table1.Code to Table2.Code?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
yes please.
Thanks
UPDATE  Table2
SET     Code  = t1.Code
FROM    Table1 t1 LEFT OUTER JOIN Table2 t2
ON      t1.Ref = t2.Ref

Open in new window


But it won't help you to add missed values of Ref into Table2. You should insert missed records from Table1 into Table2

insert into Table2 (select Ref, Code from Table1 t1 where not exists (select 1 from Table2 t2 where t1.Ref = t2.Ref)

Open in new window

 
And then you actually don't need outer join

UPDATE  Table2
SET     Code  = t1.Code
FROM    Table2 t2 INNER JOIN Table1 t1
ON      t1.Ref = t2.Ref

Open in new window

Author

Commented:
Perfect thanks!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial