我正在使用Microsoft Access 2010 我试图执行此查找的表是
StockCount
哪个包含
Unit No
Event ID
Product Name
Price
Qty Open
Qty Trnasferred
Qty Closed
Wastage
Qty Staff
Qty Sold
Total Amount Taken
Transfer
其中包含
ID
Transaction
Event ID
Unit No
Docket ID
Product Name
Amount
在StockCount.[Qty Transferred]
字段中,需要进行查询,以计算两个表中产品名称,事件ID和单位编号匹配的所有Transfer.Amount
的总和。
我当前的SQL命令
SELECT Sum(Transfer.[Amount])
FROM Transfer, StockCount
WHERE Transfer.[Product Name] = StockCount.[Product Name]
AND Transfer.[Event ID] = StockCount.[Event ID]
AND Transfer.[Unit No] = StockCount.[Unit No];
这已被放置在Qty Transferred字段的查找选项
下在此之后,没有任何值正在改变,我不确定为什么。
答案 0 :(得分:1)
SELECT语句选择记录但不更改任何数据。 SELECT语句只返回一个值,即所有金额的总和。您可能希望计算每[Product Name]
,[Event ID]
和[Unit No]
的总计。执行此操作的SELECT查询如下所示
SELECT
[Product Name], [Event ID], [Unit No],
Sum(Amount) AS SumOfAmount
FROM
Transfer
GROUP BY
[Product Name], [Event ID], [Unit No];
不幸的是,此查询无法在Access中转换为UPDATE查询,因为它包含GROUP BY
。 UPDATE查询需要将此查询加入StockCount
表。生成的查询不可更新。
我看到了这个问题的两个解决方案
使用此查询的结果填写工作表并从此表中更新
通过循环查询上面的查询结果,以编程方式更新StockCount
表。
我在这里展示了第一种方法。首先创建工作表。
twrkTotalAmount --------------- Product Name Event ID Unit No SumOfAmount
然后清除表格(它可能包含先前运行的记录)
DELETE * FROM twrkTotalAmount;
现在插入总和
INSERT INTO twrkTotalAmount
([Product Name], [Event ID], [Unit No], SumOfAmount)
SELECT
[Product Name], [Event ID], [Unit No], Sum(Amount)
FROM
Transfer
GROUP BY
[Product Name], [Event ID], [Unit No];
现在您可以执行UPDATE查询
UPDATE
StockCount
INNER JOIN twrkTotalAmount
ON (StockCount.[Product Name] = twrkTotalAmount.[Product Name]) AND
(StockCount.[Event ID] = twrkTotalAmount.[Event ID]) AND
(StockCount.[Unit No] = twrkTotalAmount.[Unit No])
SET
StockCount.[Total Amount Taken] = [SumOfAmount];