Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hi guys,

In Sql server 2008 r2.

I have two table with same no.of cols and cols name and its data type.

scenario
tableOne
------------------------------------
|col1  | col2       | date         |
|----------------------------------|
| A    | Apple      | 10/01/2014   |
|----------------------------------|
| B    | Ball       | 10/01/2014   |
------------------------------------

tableTwo
------------------------------------
|col1  | col2       | date         |
|----------------------------------|
| A    | Apple      | 10/02/2014   |
|----------------------------------|
| B    | Bat        | 10/02/2014   |
------------------------------------


I want to select those records which are not matching.

for eg: row 'A' of tableOne is matching with row 'A' of tableTwo,
but row 'B' of tableOne is not matching with row 'B' of tableTwo.

I want to select row 'B' from tableTwo because it is not matching.

The above tables are just given for scenario, in real time i have 1000's of records
in both the tables.

Can anyone please help me...

Thanks
Posted
Updated 19-Nov-14 20:40pm
v4
Comments
Hasham Ahmad 19-Nov-14 13:48pm    
can you also write down the result what you want to achieve?
DaveAuld 19-Nov-14 13:51pm    
I would think the implementation is looking for 'B' on this definition because each day 'A' is 'Apple' but 'B' has changed. Could be wrong though!
abdul subhan mohammed 19-Nov-14 13:56pm    
i want to select b which is not matching
Zoltán Zörgő 19-Nov-14 13:49pm    
Quite unclear. Please try to give a much better example, and explain the logic better.
abdul subhan mohammed 19-Nov-14 13:55pm    
i want to select b, which is not matching

Try this

SQL
select * from tableTwo except select * from tableOne
 
Share this answer
 
v2
Assuming you have the two dates in variables named @currDate and @prevDate. Further assuming there are no deleted or new records. Even further assuming that col2 is not null.
SQL
select curr.col1, curr.col2 as col2_curr, prev.col2 as col2_prev
from [table] curr
inner join [table] prev on prev.col1 = curr.col1
where curr.date = @currDate
and prev.date = @prevDate
and curr.col2 <> prev.col2
 
Share this answer
 
Comments
Maciej Los 19-Nov-14 17:12pm    
Tomas, sorry, but this is not generic issue. OP wants to "filter" thousands of records.
Tomas Takac 19-Nov-14 17:26pm    
OK, I admit I don't understand what he means by that.

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