简体   繁体   English

提取两个表之间的差异-MS Access SQL查询

[英]Extracting differences between two tables - MS Access SQL Query

What I need to do is display all differences between the two tables; 我需要做的是显示两个表之间的所有差异。 All the records that exist in TABLE1 but not TABLE2, the records that exist in TABLE2 but not TABLE1, and the records that don't have a matching Amount in the other table. TABLE1中存在但TABLE2中不存在的所有记录,TABLE2中存在但TABLE1中不存在的记录,以及另一个表中没有匹配的Amount的记录。

I have two tables: 我有两个表:

(TABLE 1)
CO_CODE   ACCOUNT_ID   CONTRACT    STATUS          AMT
A         7              101       ACTIVE          5,497.00
A         7              101       ACTIVE          5,482.00
A         15             106       INACTIVE        0.00
A         23             102       ACTIVE          4,562.00
A         31             102       ACTIVE          22.00
A         49             103       ACTIVE          1,900.00
A         49             103       ACTIVE          135.00

(TABLE 2)
Company      Account_Number    Amount      Agreement
A            7                 5,497.00    S000101
A            23                8,457.00    S000102
A            49                135.00      S000103          
A            56                2,465.00    S000104              
A            7                 5,482.00    S000101

The two tables share the fields with the Company ID, Account Number, Amount, and Agreement/Contract# where the Agreement in TABLE2 is type Text but is numerical in TABLE1. 这两个表共享具有公司ID,帐号,金额和协议/合同号的字段,其中表2中的协议为文本类型,而表1中为数字类型。

So far, I've come up with: 到目前为止,我想出了:

SELECT TABLE1.CO_CODE AS Company, TABLE1.ACCOUNT_ID AS [Account Number], TABLE1.CONTRACT As [Contract Number], TABLE1.STATUS AS Status, TABLE1.AMT, TABLE2.Amount
FROM TABLE1 LEFT JOIN TABLE2 ON TABLE1.ACCOUNT_ID = TABLE2.Account_Number
WHERE ((Exists (SELECT * FROM TABLE2 WHERE TABLE1.CO_CODE = TABLE2.Company AND TABLE1.ACCOUNT_ID = TABLE2.Account_number AND TABLE1.CONTRACT = Clng(MID(TABLE2.Agreement,5)) AND TABLE1.AMT = TABLE2.Amount))=False) OR ((Exists (SELECT * FROM TABLE1 WHERE TABLE1.CO_CODE = TABLE2.Company AND TABLE1.ACCOUNT_ID = TABLE2.Account_number AND TABLE1.CONTRACT = Clng(MID(TABLE2.Agreement,5)) AND TABLE1.AMT = TABLE2.Amount))=False)

UNION ALL 

SELECT TABLE2.Company, TABLE2.Account_Number, CLng(MID(TABLE2.Agreement, 5)), TABLE1.ACC_STATUS, TABLE1.AMT, TABLE2.Amount
FROM TABLE2 LEFT JOIN TABLE1 ON TABLE1.ACCOUNT_ID = TABLE2.Account_Number 
WHERE TABLE1.ACCOUNT_ID NOT IN (SELECT Account_Number FROM TABLE2) OR TABLE2.Account_Number NOT IN (SELECT ACCOUNT_ID FROM TABLE1);

This gives me the result: 这给了我结果:

Company  Account    Contract    Status       AMT           Amount
A         15         106        INACTIVE     0.00   
A         23         102        ACTIVE       4,562.00      8,457.00
A         31         102        ACTIVE       22.00    
A         49         103        ACTIVE       1,900.00      135.00
A         56         104                                   2,465.00

Account 49 with an amount of 135.00 should not show the amount of 135.00, as it occurs in both tables, however the amt of 1900.00 is correct (it only appears in TABLE1). 金额为135.00的帐户49不应显示金额135.00,因为它在两个表中都出现,但是1900.00的amt是正确的(仅出现在表1中)。

Could someone tell me why this is happening and how to fix it? 有人可以告诉我为什么会这样以及如何解决吗?

I apologize in advance for my lack of knowledge in MS Access SQL and this website in general. 对于缺乏MS Access SQL和整个网站的知识,我深表歉意。 I've been struggling the past week trying to figure out how to obtain the desired results and any help would be appreciated. 在过去的一周中,我一直在努力寻找如何获得期望的结果,任何帮助将不胜感激。

Disclaimer: I am by no means an expert with SQL, so my solution may be inefficient or even plain wrong, but it was working on my own testing environment. 免责声明:我绝不是SQL方面的专家,因此我的解决方案可能效率不高甚至是错误的,但是它在我自己的测试环境中有效。

Try the following: 请尝试以下操作:

SELECT T1.CO_CODE AS Company, T1.ACCOUNT_ID AS [Account Number], T1.STATUS AS Status,  T1.CONTRACT As [Contract Number], T1.AMT AS [Table 1], "" AS [Table 2]    
FROM TABLE1 T1
WHERE NOT EXISTS (
SELECT * FROM TABLE2 T2 
WHERE T1.CO_CODE = T2.Company 
AND T1.Account_ID = T2.Account_Number
AND T1.CONTRACT = CLng(MID(TABLE2.Agreement, 5))
AND T1.AMT = T2.Amount
)

UNION ALL

SELECT T2.Company, T2.Account_Number AS [Account Number], "" AS Status, CLng(MID(TABLE2.Agreement, 5)) AS [Contract Number],  "" AS [Table 1], T2.Amount AS [Table 2]    
FROM TABLE2 T2
WHERE NOT EXISTS (
SELECT * FROM TABLE1 T1 
WHERE T1.CO_CODE = T2.Company 
AND T1.Account_ID = T2.Account_Number
AND T1.CONTRACT = CLng(MID(TABLE2.Agreement, 5))    
AND T1.AMT = T2.Amount
)

Which results in the following: 结果如下:

-----------------------------------------------------------------
|Company|Account Number| Status |Contract Number|Table 1|Table 2|
-----------------------------------------------------------------
|   A   |      15      |INACTIVE|      106      | 0     |       |
|   A   |      23      | ACTIVE |      102      | 4562  |       |
|   A   |      31      | ACTIVE |      102      | 22    |       |
|   A   |      49      | ACTIVE |      103      | 1900  |       |
|   A   |      23      |        |      102      |       | 8457  |
|   A   |      56      |        |      104      |       | 2465  |
-----------------------------------------------------------------

I didn't know what types each of your columns were, so the query may need to be modified a bit to match your data. 我不知道您的每一列是什么类型,因此可能需要对查询进行一些修改以匹配您的数据。 You may also want to rename the columns to something else, I was just throwing in data. 您可能还想将列重命名为其他名称,我只是在输入数据。

Notice that records where there are two different amounts in each table show up twice (ie Account #23 from your example). 请注意,每个表中有两个不同金额的记录显示了两次(即,示例中的帐户#23)。 I don't know if this is acceptable behaviour or not for you, but it is what I came up with. 我不知道这对您来说是否可以接受,但这是我想出的。

I hope this helps, or at least gives you something else to try. 我希望这会有所帮助,或者至少会为您提供其他尝试。

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

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