我正在尝试比较两个库存清单;当月与上个月相比。这些数据存放在两个单独的表中。
两个架构略有不同,一个文件使用double
表示数值,而另一个文件使用decimal
。
我想比较添加或删除新ItemID的位置,与上个月的数据进行比较,以及值的变化。
Access中是否可以这样,或者我是否更适合将数据导入MS SQL Server?
此外,这种比较可以仅通过查询完成,还是我需要编写一些VBA?
答案 0 :(得分:1)
MS Access非常乐意比较十进制和双数
SELECT "In 1" As Diff, Invent1.ID, Invent1.Amount, Invent2.Amount
FROM Invent1 LEFT JOIN Invent2 ON Invent1.ID = Invent2.ID
WHERE Invent2.ID Is Null
UNION
SELECT "In 2" As Diff, Invent2.ID, Invent1.Amount, Invent2.Amount
FROM Invent2 LEFT JOIN Invent1 ON Invent2.ID = Invent1.ID
WHERE Invent1.ID Is Null
UNION
SELECT "Amount" As Diff, Invent1.ID, Invent1.Amount, Invent2.Amount
FROM Invent1 INNER JOIN Invent2 ON Invent1.ID = Invent2.ID
WHERE [Invent1].[Amount]<>[Invent2].[Amount]
您可能希望更改最后一个联合,具体取决于您的数据:
SELECT "Amount" As Diff, Invent1.ID, Invent1.Amount, Invent2.Amount
FROM Invent1 INNER JOIN Invent2 ON Invent1.ID = Invent2.ID
WHERE CCur([Invent1].[Amount])<>CCur([Invent2].[Amount])
或更好:
UNION SELECT "Amount" As Diff, Invent1.ID, Invent1.Amount, Invent2.Amount
FROM Invent1 INNER JOIN Invent2 ON Invent1.ID = Invent2.ID
WHERE CCur(Nz([Invent1].[Amount],0))<>CCur(Nz([Invent2].[Amount],0));
答案 1 :(得分:0)
我会做类似的事情:
SELECT
t1.*,
t2.*
FROM Table1 t1
FULL OUTER JOIN Table2 t2 ON t2.Id = t1.Id
WHERE 1=1
AND t1.Field1 <> t2.Field1
AND t1.Field2 <> t2.Field2
etc.
这将为您提供原始数据。然后,您可以添加到WHERE以过滤NULLS或其他任何内容。左表中缺少的条目将指示已添加的内容。右表(t2)上缺少的条目将显示删除的内容。
我不确定msaccess中是否有FULL OUTER JOINS,但它们应该在查询设计器中:http://www.databasejournal.com/features/msaccess/article.php/3516561/Implementing-the-Equivalent-of-a-FULL-OUTER-JOIN-in-Microsoft-Access.htm