简体   繁体   中英

SQL Query to find missing records between two tables that share a key

Just as the title asks I am trying to find a way to give me a number for the difference in transactions. The question in plain English is: There is some bad data in the database. Some sales transactions are missing data on which product and how many were sold.

Salestransaction (Table 1) Columns: transactionid, customerid, storeid, tdate

Soldvia (Table 2) Columns: transactionid, productid, noofitems

Any help on this would be appreciated. If I am being unclear let me know and I will provide more info. Thank you.

This is simple LEFT JOIN with filtering null s:

For missing items in Soldvia table:

select count(*) as difference 
from Salestransaction st
left join Soldvia s on st.transactionid = s.transactionid 
where s.transactionid is null

For missing items in both tables:

select count(*) as difference 
from Salestransaction st
full join Soldvia s on st.transactionid = s.transactionid 
where st.transactionid is null or s.transactionid is null

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