简体   繁体   English

如何求和另一列等于其他表的列

[英]How to sum a column where another column is equal to other table

I have 2 tables, Assets and Main . 我有2个表格, AssetsMain 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 ,我有AccountDescript列。 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 ,我有AmtAc1Ac2

  • Amt holds amounts that we need to sum Amt持有我们需要求和的金额
  • Ac1 and Ac2 hold account numbers from Assets as text Ac1Ac2以文本形式保存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中:

  1. You have -1000.00 in "Amt", you have "1001" in "Ac1", and "1101" in "Ac2". 在“ Amt”中具有-1000.00,在“ Ac1”中具有“ 1001”,在“ Ac2”中具有“ 1101”。
  2. You have 2000.00 in "Amt", you have "1001" in "Ac1", and "1101" in "Ac2". 在“ Amt”中有2000.00,在“ Ac1”中有“ 1001”,在“ Ac2”中有“ 1101”。

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.

相关问题 从表中选择数据,其中一列中的值总和等于另一列中的值 - Selecting data from table where sum of values in a column equal to the value in another column 更新表中的列以等于同一表中的另一列WHERE NOT语法 - Updating column in table to equal another column in same table WHERE NOT syntax 如何从列等于一个值但不应等于另一个值的表中选择记录? - How to SELECT record from a table where column is equal to a value but shouldn't be equal to another value? 如何将值插入到另一列等于特定单词的列中? - How to insert a value to a column where another column is equal to specific word? SQL组列,其中其他列相等 - SQL group column where other column is equal MySql 获取其中一列等于另一列等于另一表中的另一列的行 - MySql get rows where one column equals to another colum in another table where one column is equal 如何根据另一表的列更新表的一列的总和 - how to update Sum of one column of table based on column of other table 一个表中的SQL sum列,其中另一表中的值为“ S” - SQL sum column in one table where value in another table is 'S' 使用另一个表中的相同列更新列,并使用另一个表进行gruoping并使sum相等 - Update column using the same column from another table and gruoping by another and make sum equal 1个表组中某一列的总和与另一表中2个其他列的和 - Sum of a column in 1 table group by 2 other columns in another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM