[英]How to sum a column where another column is equal to other table
I have 2 tables, Assets
and Main
. 我有2个表格,
Assets
和Main
。 I want to create a query that will total the transactions in Main
, grouped by each account in Assets
. 我想创建一个查询,该查询将对
Main
的交易进行总计,并按Assets
的每个帐户进行分组。 But there's a catch: sometimes the amt
needs to be summed as a positive and sometimes as a negative. 但是有一个陷阱:有时需要将
amt
总结为肯定的,有时需要总结为否定的。
In Assets
, I have the columns Account
and Descript
. 在
Assets
,我有Account
和Descript
列。 Account
holds "1001", and others, as text; Account
包含“ 1001”和其他文字。 Descript
is just text. Descript
只是文本。
Account Descript
--------------------------
1001 Cash
1101 Receivable
In Main
, I have Amt
, Ac1
, and Ac2
. 在
Main
,我有Amt
, Ac1
和Ac2
。
Amt
holds amounts that we need to sum Amt
持有我们需要求和的金额 Ac1
and Ac2
hold account numbers from Assets
as text Ac1
和Ac2
以文本形式保存Assets
帐号 In Main
, when an account is marked in Ac1
, the transaction is a positive for that account. 在
Main
,当在Ac1
标记一个帐户时,该交易对该帐户是肯定的。 When an account is marked in Ac2
, the transaction amount is a negative for that account. 在
Ac2
标记一个帐户后,该帐户的交易金额为负数。
Say, for one record, in Main: 说一句,在Main中:
Data: 数据:
Amt Ac1 Ac2
-------------------
-1000 1001 1101
2000 1001 1101
so then the expected result needs to be: 因此,预期结果需要为:
Account Descrip TtlAmt
-------------------------------
1001 Cash 1000.00
1101 Receivable -1000.00
I have some code but I'm not sure if it's helpful. 我有一些代码,但不确定是否有帮助。
SELECT
Asset.Account, Asset.Descrip AS Expr1,
SUM(Main.Amt) AS SumOfAMT, SUM(Main.Amt) AS Expr2
FROM
Asset
LEFT JOIN
Main ON (Asset.ACCOUNT = Main.AC2) OR (Asset.ACCOUNT = Main.AC1)
GROUP BY
Asset.Account, Asset.Descrip;
Just to be super clear, I also have tables called "Liability", "Expense", etc. But I felt that we can focus on just the one query here, as the rest should fall into place with some guidance. 只是为了非常清楚,我也有称为“责任”,“费用”等的表。但是我觉得我们可以只关注一个查询,因为其他查询应该在适当的指导下就位。
I know this has nothing to do with problem at hand, but in Excel I use the following formula to accomplish this. 我知道这与手头的问题无关,但是在Excel中,我使用以下公式来完成此任务。
SUM(SUMIF([sum range], [criteria range], [criteria]), SUMIF([sum range], [criteria range], [criteria])*-1)
I thought it may be helpful to explain my end goal. 我认为解释我的最终目标可能会有所帮助。
Assuming there is a unique identifier field in Main. 假设Main中有一个唯一的标识符字段。
Consider: 考虑:
Query1 查询1
SELECT ID, "Ac1" AS Src, Ac1 AS Act, Amt FROM Main
UNION SELECT ID, "Ac2", Ac2, Amt*-1 FROM Main;
Query2 QUERY2
SELECT Query1.Act, Assets.Descrip, Sum(Query1.Amt) AS SumOfAmt
FROM Assets INNER JOIN Query1 ON Assets.Account = Query1.Act
GROUP BY Query1.Act, Assets.Descrip;
All in one 一体
SELECT Query1.Act, Assets.Descrip, Sum(Query1.Amt) AS SumOfAmt
FROM Assets INNER JOIN
(SELECT Ac1 AS Act, Amt FROM Main
UNION SELECT Ac2, Amt*-1 FROM Main) AS Query1
ON Assets.Account = Query1.Act
GROUP BY Query1.Act, Assets.Descrip;
From a performance perspective, correlated subqueries might be a better approach: 从性能的角度来看,关联子查询可能是一种更好的方法:
select a.*,
( (select nz(sum(m.amt), 0)
from main as m
where m.ac1 = a.account
) -
(select nz(sum(m.amt), 0)
from main as m
where m.ac2 = a.account
)
) as net_amount
from assets as a;
In particular, this can take advantage of two indexes: main(ac1, amt)
and main(ac2.amt)
. 特别是,这可以利用两个索引:
main(ac1, amt)
和main(ac2.amt)
。
In addition, it eliminates the aggregation on the entire result set. 此外,它消除了整个结果集的聚合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.