简体   繁体   English

SQL查询以查找共享键的两个表之间的丢失的记录

[英]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 Salestransaction(表1)列:transactionid,customerid,storeid,tdate

Soldvia (Table 2) Columns: transactionid, productid, noofitems Soldvia(表2)栏: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: 这是简单的LEFT JOIN其中过滤了null

For missing items in Soldvia table: 对于Soldvia表中的缺失物品:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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