Fordraiders
asked on
trying to find the differences between 2 tables based on 2 fields..
trying to find the differences between 2 tables based on 2 fields..
project_number
and
Sku
and return records from the table where records are missing from the other table
but i keep getting back repeated records.
select * from [Metrics].[dbo].[Header_Cu rrent_miss ing]
inner join
[Metrics].[dbo].Header_Det ail_Curren t
on
[Metrics].[dbo].Header_Cur rent_missi ng.PROJECT _NUMBER = [Metrics].[dbo].[Header_De tail_Curre nt].PROJEC T_NUMBER
and
[Metrics].[dbo].Header_Cur rent_missi ng.sku <> [Metrics].[dbo].[Header_De tail_Curre nt].SKU
Like this: Header_Current_missing
project_number sku
111112 2a228
222221 2e342
444445 2x454
Like this: Header_Detail_Current
project_number sku
111112 2a223
222221 2e342
444445 2x454
So the query should return this one record.
Like this: Header_Current_missing
project_number sku
111112 2a228
Thanks
fordraiders
project_number
and
Sku
and return records from the table where records are missing from the other table
but i keep getting back repeated records.
select * from [Metrics].[dbo].[Header_Cu
inner join
[Metrics].[dbo].Header_Det
on
[Metrics].[dbo].Header_Cur
and
[Metrics].[dbo].Header_Cur
Like this: Header_Current_missing
project_number sku
111112 2a228
222221 2e342
444445 2x454
Like this: Header_Detail_Current
project_number sku
111112 2a223
222221 2e342
444445 2x454
So the query should return this one record.
Like this: Header_Current_missing
project_number sku
111112 2a228
Thanks
fordraiders
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks all !!
The reasons you are having problems are:
1. the use of INNER JOIN rather than LEFT JOIN. Inner join will only return records from the two tables which match based on the Join criteria. An outer join (LEFT JOIN) will return all of the records from the "first" table and only those records which match from the second table.
2. The use of the "<>" in your join criteria. This will return all record from the two tables which have the same project number, but different skus.
So with this <> in the join, and data that looks like:
SELECT T1.*, T2.* FROM Table1 as T1 INNER JOIN Table2 as T2
ON T1.ProjNum = T2.ProjNum AND T1.ProjNum <> T2.ProjNum
Open in new window