Solved Totals Query

ClaraBarton

Registered User.
Local time
Yesterday, 22:11
Joined
Oct 14, 2019
Messages
606
Code:
SELECT qryTransactions.TransactionID,
    qryTransactions.Cleared,
    Nz(-[Credit],0)+Nz([debit],0) AS CkAmount,
    -Nz([CatCredit],0)+Nz([CatDebit],0) AS CatAmount,
    qryTransfers.TAmount,
    Sum(IIf([Cleared]=True,+[CkAmount],0)) AS ClearedAmt,
    Sum(Nz([CkAmount],0)-Nz([CatAmount],0)+Nz([TAmount],0)) AS Remaining
FROM (qryTransactions LEFT JOIN tblCheckCat ON qryTransactions.TransactionID = tblCheckCat.fTransactionID) LEFT JOIN qryTransfers ON qryTransactions.TransactionID = qryTransfers.TTransID
GROUP BY qryTransactions.TransactionID,
    qryTransactions.Cleared,
    Nz(-[Credit],0)+Nz([debit],0),
    -Nz([CatCredit],0)+Nz([CatDebit],0),
    qryTransfers.TAmount;
1733348798194.png

1733348810407.png

I would like for Transaction19 to return 10 in the ClearedAmt and 10 in the Remaining which is what's happening in TransID 21.
What is the difference?
 
Can you post the sample data that is driving this query?
You show the end results, but not the data itself.
 
Like this?
1733350710977.png
 

Attachments

  • 1733350583824.png
    1733350583824.png
    13.6 KB · Views: 11
You have CkAmount in a source as a column and then define it again in your query. You also reference CkAmount in two other expressions in your query. This creates an opportunity for confusion.

1733351888264.png
 
Without seeing the raw data , my guess would be you have two records with a transactionID of 19
 
For your ClearedAmt, you don't need to do a SUM on it.
ClearedAmt: IIf([Cleared]=True,[CkAmount],0)
 
I removed the CkAmount from qryTransactions. If I remove the Sum from Cleared It says Does not include as a part of an Aggregated function. The only thing it will accept is sum. There are not 2 ID's 19. It is an autonumber.
 
Wow! You nailed it! It's a foreign key with nothing in it but it's in there twice. Thank you so much!
 

Users who are viewing this thread

Back
Top Bottom