我有一个与MAS200数据库异步工作的Access 2007数据库。由于各种办公室政治和繁文缛节,我无法访问MAS200数据,即使ODBC使其变得简单,并且必须将数据转储从MAS200导入到Access。由于这个以及报告的运行方式,我偶尔会在转储上有重复的数据。
我的解决方案是拥有一个DATA表和一个DUMP表。现在我需要找到正确的查询,以便在导入之前检查来自DUMP的行是否存在于DATA中。
两个表都是这样的,加上一些在dupe和dump逻辑完成后手动填写的字段:
DATA ID / DUMP ID
InvoiceDate
InvoiceNumber
CompanyID
Product
Description
SalespersonID
Quantity
Cost
Price
我的假设是我可以使用以下查询,但它似乎不起作用。
INSERT INTO tblDATA(InvoiceDate, InvoiceNumber, CompanyID, Product, Description, SalespersonID, Quantity, Cost, Price)
SELECT tblDUMP(InvoiceDate, InvoiceNumber, CompanyID, Product, Description, SalespersonID, Quantity, Cost, Price)
WHERE tblDATA.InvoiceNumber != tblDUMP.InvoiceNumber AND tblDATA.Product != tblDUMP.Product AND tblDATA.Quantity != tblDUMP.Quantity
您可以修复我的代码或向我展示更好的路径吗?
EDIT!
自从我第一次发布此问题以来,我的数据库略有变化。我正在考虑纠正的问题现在是:
INSERT INTO tblSold(InvoiceDate, Invoice, CompanyID, Product, Description, Salesperson, Quantity, Cost, Price)
SELECT tblNewData.InvoiceDate, tblNewData.Invoice, tblNewData.CompanyID, tblNewData.Product, tblNewData.Description, tblNewData.Salesperson, tblNewData.Quantity, tblNewData.Cost, tblNewData.Price
FROM tblNewData FULL OUTER JOIN tblSold ON tblSold.InvoiceNumber = tblNewData.InvoiceNumber AND tblSold.Product = tblNewData.Product AND tblSold.Quantity = tblNewData.Quantity
WHERE tblSold.InvoiceNumber IS NULL AND tblSold.Product IS NULL AND tblSold.Quantity IS NULL
答案 0 :(得分:0)
一位朋友给我写了一个很棒的答案。
INSERT INTO tblDATA (InvoiceDate, InvoiceNumber, CompanyID, Product, Description, SalespersonID, Quantity, Cost, Price)
SELECT tblDUMP.InvoiceDate, tblDUMP.InvoiceNumber, tblDUMP.CompanyID, tblDUMP.Product, tblDUMP.Description, tblDUMP.SalespersonID, tblDUMP.Quantity, tblDUMP.Cost, tblDUMP.Price
FROM tblDUMP FULL OUTER JOIN tblDATA ON tblDUMP.InvoiceNumber = tblDATA.InvoiceNumber AND tblDUMP.Product = tblDATA.Product AND tblDUMP.Quantity = tblDATA.Quantity
WHERE tblDATA.InvoiceNumber IS NULL AND tblDATA.Product IS NULL AND tblDATA.Quantity IS NULL
注意到语法在Access中不完美。
我找到了一些很棒的研究,让我了解他的理由: What is the difference between Left, Right, Outer and Inner Joins? https://blog.codinghorror.com/a-visual-explanation-of-sql-joins/
现在我需要Access 2007语法的帮助。
答案 1 :(得分:0)
Access不支持FULL OUTER JOIN。我必须使用左边的join和union,如下所述:http://bytes.com/topic/access/answers/642644-excel-vlookup-full-outer-join