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

时间:2014-02-05 14:37:14

标签: sql database ms-access join union

我需要做的是显示两个表之间的所有差异; TABLE1中存在但不存在于TABLE2中的所有记录,TABLE2中存在但不存在于TABLE1中的记录,以及在另一个表中没有匹配的Amount的记录。

我有两张桌子:

(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

这两个表共享带有公司ID,帐号,金额和协议/合同#的字段,其中TABLE2中的协议类型为Text,但在TABLE1中是数字。

到目前为止,我已经提出:

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);

这给了我结果:

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

金额为135.00的账户49不应显示135.00的金额,因为它出现在两个表中,但是1900.00的金额是正确的(它只出现在TABLE1中)。

有人可以告诉我为什么会发生这种情况以及如何解决这个问题吗?

我提前为MS Access SQL和本网站缺乏知识而道歉。过去一周我一直在努力想弄清楚如何获得理想的结果,我们将不胜感激。

1 个答案:

答案 0 :(得分:0)

免责声明:我绝不是SQL的专家,因此我的解决方案可能效率低下甚至是错误的,但它正在我自己的测试环境中工作。

尝试以下方法:

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
)

结果如下:

-----------------------------------------------------------------
|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  |
-----------------------------------------------------------------

我不知道每个列的类型是什么,因此可能需要稍微修改一下查询以匹配您的数据。您可能还想将列重命名为其他内容,我只是在抛出数据。

请注意,每个表中有两个不同金额的记录会显示两次(即您示例中的帐户#23)。我不知道这对你来说是否是可接受的行为,但这是我想出来的。

我希望这会有所帮助,或至少给你一些其他的尝试。