[英]SQL Query to find missing records between two tables that share a key
就像標題要求的那樣,我正在嘗試尋找一種方法來給我一些交易差異的數字。 用簡單的英語來說的問題是:數據庫中有一些不良數據。 一些銷售交易缺少有關哪種產品以及售出了多少的數據。
Salestransaction(表1)列:transactionid,customerid,storeid,tdate
Soldvia(表2)欄:transactionid,productid,noofitems
任何幫助,將不勝感激。 如果我不清楚,請告訴我,我將提供更多信息。 謝謝。
這是簡單的LEFT JOIN
其中過濾了null
:
對於Soldvia
表中的缺失物品:
select count(*) as difference
from Salestransaction st
left join Soldvia s on st.transactionid = s.transactionid
where s.transactionid is null
對於兩個表中的缺失項:
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.