如何在访问中按总和查询总和

时间:2013-12-02 15:32:32

标签: ms-access ms-access-2007

我有这样的查询::

SELECT account.AccountNumber, account.NAME, Sum(agro.price * agro.qty) AS Expr1
FROM ((account
INNER JOIN data ON account.AccountNumber = data.acno)
INNER JOIN agro ON agro.BillNo = data.BillNo)
WHERE data.db='true'
GROUP BY account.AccountNumber, account.NAME;

我想扣除另一个groupby查询输出到 Sum(agro.price * agro.qty)这个 查询的另一个组是 SELECT Sum(rs),acno 来自jma group by acno; 我想扣除Sum(agro.price * agro.qty)-Sum(rs)它的工作原理请帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

如果我理解正确,以下查询可能适合您:

SELECT subQ.AccountNumber, subQ.NAME, (subQ.subSum - jmaSum.jSum) AS FinalSum
FROM 
(
  SELECT a.AccountNumber, a.NAME, Sum(ag.price * ag.qty) AS subSum
  FROM (account AS a
    INNER JOIN data AS d ON a.AccountNumber = d.acno)
    INNER JOIN agro AS ag ON ag.BillNo = d.BillNo
  WHERE d.db = 'true'
  GROUP BY a.AccountNumber, a.NAME
) AS subQ
LEFT JOIN 
(
  SELECT Sum(j.rs) AS jSum, j.acno 
  FROM jma AS j
  GROUP BY j.acno
) AS jmaSum ON subQ.AccountNumber = jmaSum.acno