简体   繁体   中英

Eliminating non matching rows dependent on specific columns between two tables using SQL

I have two tables where I want to match only on specific columns such as finding matching account numbers. I have that portion figured out however two of the columns are calltime and calldate . I want to find all records that are a discrepancy (found in one but not the other). The issue is that there may be multiple calls in a day so the below is not working to find discrepancies.

Query 1 to get all calls where call date matches but is not found. The issue with this is that there may be a match for call date and call time but it's just not the current record being compared

SELECT *
FROM dbo.VDMTable
WHERE EXISTS (SELECT * 
              FROM DataopsTable
              WHERE DataopsTable.Citibank = VDMTable.Citiaccount
                AND DataopsTable.CallDate = VDMTable.Calldate
                AND DataopsTable.CallTime <> VDMTable.Calltime

You are looking for rows where exists a mismatch , and as you say, such mismatches are not really what you are looking for. What you are really interested in are rows for which not exists a match .

FROM dbo.VDMTable
WHERE NOT EXISTS
  (
    SELECT null
    FROM DataopsTable
    WHERE DataopsTable.Citibank = VDMTable.Citiaccount
    AND DataopsTable.CallDate = VDMTable.Calldate
    AND DataopsTable.CallTime = VDMTable.Calltime
)

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