简体   繁体   中英

finding unmatched data from two tables

I'm trying write a query to find records which don't have a matching record in another table.

For example, I have a two tables whose structures looks something like this:

Table1

Address ID | Address Type
AD7233654242 | Condo
AD7233654242 | Condo
AD7233654243 | Apartment
AD7233654244 | Condo

Table2

Address ID | Address Type
AD7233654242 | Condo
AD7233654242 | Apartment
AD7233654243 | Apartment
AD7233654244 | Condo

Based on the data from above you'll notice address ID AD7233654242 has a mismatch Address Type. Table A shows Condo and Table B shows Apartment. So in the query result I want to dislay the Address ID and Address Type from both of the tables.

Any suggestions on a query to do this?

You can query matching and not matching records using join type. Below are examples of different joins.

declare @tbl1 table(AddressID varchar(20), AddressType varchar(50))
declare @tbl2 table(AddressID varchar(20), AddressType varchar(50))

insert @tbl1(AddressID,AddressType) values
('AD7233654242', 'Condo'),
('AD7233654242', 'Condo'),
('AD7233654243', 'Apartment'),
('AD7233654244', 'Condo'),
('AD7233654245', 'Condo')--my sample

insert @tbl2(AddressID,AddressType) values
('AD7233654242', 'Condo'),
('AD7233654242', 'Apartment'),
('AD7233654243', 'Apartment'),
('AD7233654244', 'Condo')

--records in @tbl1 matching @tbl2
select t1.AddressID addrId_1, t1.AddressType addrType_1, t2.AddressID addrId_2, t2.AddressType addrType_2
from @tbl1 t1 -- 1st or left table
inner join @tbl2 t2 --2nd or right table
on t1.AddressID=t2.AddressID and t1.AddressType=t2.AddressType

--records in @tbl1 not matching @tbl2
select t1.AddressID, t1.AddressType
from @tbl1 t1
left join @tbl2 t2 on t1.AddressID=t2.AddressID and t1.AddressType=t2.AddressType
where t2.AddressID is null

--records in @tbl2 not matching @tbl1
select t2.AddressID, t2.AddressType
from @tbl1 t1
right join @tbl2 t2 on t1.AddressID=t2.AddressID and t1.AddressType=t2.AddressType
where t1.AddressID is null

--all mismatches
select t1.AddressID addrId_1, t1.AddressType addrType_1, t2.AddressID addrId_2, t2.AddressType addrType_2
from @tbl1 t1
full join @tbl2 t2 on t1.AddressID=t2.AddressID and t1.AddressType=t2.AddressType
where t1.AddressID is null or t2.AddressID is null

A simple join should work here:

SELECT
    t1.AddressID,
    t1.AddressType,
    t2.AddressType
FROM Table1 t1
INNER JOIN Table2 t2
    ON t1.AddressID = t2.AddressID AND t1.AddressType <> t2.AddressType;

This would actually return two records for AD7233654242 . If you have a certain expected output, we can modify the above query using that logic.

You can try using left join and null in where condition

SELECT t1.AddressID, t1.AddressType,t2.AddressType
FROM Table1 t1
left JOIN Table2 t2
    ON t1.AddressID = t2.AddressID AND t1.AddressType=t2.AddressType
where t2.AddressID is null

I guess this is what you are asking..

SELECT * from table1 T1
where exists (
SELECT * from table2 T2
where T1.AddressID = T2.AddressID
and T1.AddressType<>T1.AddressType)

UNION 

SELECT * from table1 T2
where exists (
SELECT * from table2 T1
where T1.AddressID = T2.AddressID
and T1.AddressType<>T1.AddressType)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM