Click here to Skip to main content
15,909,445 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a table with below values

table1
    id
10802000320601	
10802000320602	
10802000320603	
10802000320604	
10802000320605	
10802000320606	
10802000320607	
10802000320608	
10802000320609	
10802000320610	
10802000320611	
10802000320612	
10802000320613	

table2
     id
10802000320601	
10802000320602	
10802000320603	
10802000320604	
10802000320605	
10802000320606	

I need to compare two tables and display only matched columns and status.

Example: Output

     id                Status
10802000320601	   Matched 
10802000320602	   Matched  
10802000320603	   Matched 
10802000320604	   Matched 
10802000320605	   Matched 
10802000320606	   Matched 
10802000320607	   Unmatched 
10802000320608	   Unmatched
10802000320609	   Unmatched
10802000320610	   Unmatched
10802000320611	   Unmatched
10802000320612	   Unmatched
10802000320613	   Unmatched



Help me.
Posted
Updated 18-Apr-13 21:32pm
v2

1 solution

If Table1 contains more data then Table2, try this:
SQL
SELECT t1.id, CASE WHEN t2.id IS NULL THEN 'Unmatched' ELSE 'Matchaed' END AS 'CompareResult'
FROM Table1 AS t1 LEFT JOIN Table2 AS t2 ON t1.id = t2.ID


Idea: get all data from Table1 and only these data which are equal in table2. If id in table2 is NULL, then it means 'Unmatched'.
 
Share this answer
 
Comments
subbu a 19-Apr-13 3:44am    
it will shows error

error is

The multi-part identifier "c.CIDR_Number" could not be bound.
subbu a 19-Apr-13 3:48am    
sorry it will works. i am doing one mistake

thanks
Maciej Los 19-Apr-13 3:58am    
Please, mark this solution as "solved" (formally).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900