嵌套查询微软访问

时间:2011-04-29 19:28:47

标签: ms-access nested-queries

我有一张交易表。该表是多个供应商,具有多个交易金额的多个交易。如果供应商交易超过该供应商的平均交易金额的两倍,我需要更新该表。到目前为止,我想出了以下错误代码:

Update tblTransaction
SET VariabilityIndicator = 1
WHERE transactionNumber IN
(Select transactionNumber
From tblTransaction
GROUP BY VendorName
HAVING transactionAmount >= AVG(transactionAmount*2))

上面的代码显然是错误的。我想出了一个可能嵌套的声明:

SELECT AVG(transactionAmount) VendorName
FROM tblTransaction
GROUP BY VendorName

这应该返回所有VendorNames及其平均交易金额。如何嵌套这样我可以将transactionAmount与供应商名称匹配的平均值进行比较?

1 个答案:

答案 0 :(得分:0)

Update tblTransaction
SET VariabilityIndicator = 1
WHERE transactionNumber IN
(
    Select T.TransactionNumber
    from tblTransaction as T
    Where T.transactonAmount > 2 * (
      Select AVG(transactionAmount)
      From tblTransaction as A
      Where A.VendorName = T.VendorName
    )
)

您可能希望检查业务逻辑,以从平均计算中排除正在评估的transactionAmount。您也可能不希望匹配供应商名称,但需要使用某种ID号/主键值。