Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Table Comparison

Author  Topic 

sent_sara
Constraint Violating Yak Guru

377 Posts

Posted - 2012-01-20 : 06:16:34
Hi
can anybody help the below requirement in query form.
Requirment is i need to compare employee1 with employee2 where id is the primary key,the column value which is mismatch alone, i need to
pickit up and insert into new table


Employee1
id name age mstatus salary
2 sasi 31 M 500
3 uma 32 f 250
5 radha 25 f 600

Employee2
id name age mstatus salary
2 sasi 32 M 500
3 uma 32 M 150
5 radha1 25 f 600

final output should be:

Employee1_id Employee1_name Employee1_age Employee1_mstatus Employee1_salary Employee2_id Employee2_name Employee2_age Employee2_mstatus Employee2_salary
2 null 31 null null null null 32 null null
3 null null f 250 null null null M 150
5 radha null null null null radha1 25 null null

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-01-20 : 06:29:30
[code]SELECT
e1.id AS Employee1_Id,
CASE WHEN e1.name = e2.name THEN NULL ELSE e1.name AS Employee1_name,
CASE WHEN e1.age = e2. age THEN NULL ELSE e1.age AS Employee1_age,
--- etc for each of your columns
FROM
Employee1 e1
INNER JOIN Employee2 e2 ON e1.id = e2.id[/code]
Go to Top of Page

sent_sara
Constraint Violating Yak Guru

377 Posts

Posted - 2012-01-20 : 07:08:38
Thanks for your reply

iam trying for dynamic query.can you help me out
quote:
Originally posted by sunitabeck

SELECT
e1.id AS Employee1_Id,
CASE WHEN e1.name = e2.name THEN NULL ELSE e1.name AS Employee1_name,
CASE WHEN e1.age = e2. age THEN NULL ELSE e1.age AS Employee1_age,
--- etc for each of your columns
FROM
Employee1 e1
INNER JOIN Employee2 e2 ON e1.id = e2.id


Go to Top of Page
   

- Advertisement -