Skip to content
Advertisement

How to sum a column where another column is equal to other table

I have 2 tables, Assets and Main. I want to create a query that will total the transactions in Main, grouped by each account in Assets. But there’s a catch: sometimes the amt needs to be summed as a positive and sometimes as a negative.

In Assets, I have the columns Account and Descript. Account holds “1001”, and others, as text; Descript is just text.

In Main, I have Amt, Ac1, and Ac2.

  • Amt holds amounts that we need to sum
  • Ac1 and Ac2 hold account numbers from Assets as text

In Main, when an account is marked in Ac1, the transaction is a positive for that account. When an account is marked in Ac2, the transaction amount is a negative for that account.

Say, for one record, in Main:

  1. You have -1000.00 in “Amt”, you have “1001” in “Ac1”, and “1101” in “Ac2”.
  2. You have 2000.00 in “Amt”, you have “1001” in “Ac1”, and “1101” in “Ac2”.

Data:

so then the expected result needs to be:

I have some code but I’m not sure if it’s helpful.

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.

I thought it may be helpful to explain my end goal.

Advertisement

Answer

Assuming there is a unique identifier field in Main.

Consider:

Query1

Query2

All in one

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement