To set up the problem, I have to following 4 records in an Access table used for keeping track of dues and registrions. This Table Name is “Transactions”
ID | Transaction Date | Transaction Name | Credit Account | Debit Account | Amount
1 | 9/1/2011 | 2011 Yearly Registration | Accounts Receivable | Income : Registration | 27
2 | 2/18/2012 | Registration Payment | Cash | Accounts Receivable | 27
3 | 11/1/2012 | 2012 Yearly Registration | Accounts Receivable | Income : Registration | 27
4 | 12/7/2012 | Registration Payment | Cash | Accounts Receivable | 27
(The Transaction Name field is really an ID field that has FK to another table where the actual name is stored. That will be shown in a minute).
I then created a query that would put those records into a format where I could sum the quantities to find out balances:
SELECT [Transaction Date], [Transaction Name], (select [Account Name] from Account_lookup where ID = Transactions.[Credit Account]) as Account , sum(Amount) as [Totals]
FROM Transactions
GROUP BY [Transaction Date],[Transaction Name],[Credit Account]
UNION SELECT [Transaction Date],[Transaction Name],(select [Account Name] from Account_lookup where ID = Transactions.[Debit Account]) as Account, sum(Amount)*-1 as [Totals]
FROM Transactions
GROUP BY [Transaction Date],[Transaction Name],[Debit Account]
ORDER BY 3, 1 DESC;
The result of that is the following (The Income and Cash records have been stripped out because they’re not important for this problem). The query is called “Credit and Debit Account Totals”.
Transaction Date | Transaction Name | Account | Totals
9/1/2011 | 2011 Yearly Registration | Accounts Receivable | 27
2/18/2012 | Registration Payment | Accounts Receivable | (27)
11/1/2012 | 2012 Yearly Registration | Accounts Receivable | 27
12/7/2012 | Registration Payment | Accounts Receivable | (27)
So a query summing the Totals column should return 0. But it doesn’t.
When I run the following query on top of the query above (my query querying another query),
SELECT [Credit and Debit Account Totals].Account, Sum([Credit and Debit Account Totals].Totals) AS SumOfTotals
FROM [Credit and Debit Account Totals]
GROUP BY [Credit and Debit Account Totals].Account
HAVING ((([Credit and Debit Account Totals].Account)="Accounts Receivable"));
I get the following:
Account | SumOfTotal
Accounts Receivable | 27
If I change the sum to a count, then I get 3. I’m expecting a sum of 0, and a count of 4.
How can I get Access to use all 4 of my records? I’m guessing that somehow Access is ignoring the 2nd payment because the Transaction Name is the same, however Name isn’t part of the sum query and the Date field makes each row distinct overall in the first query.
I’ve tried putting ALL after the SELECT, but that didn’t work. Do I have to copy all the data to another table first – iow is Access getting confused by running a sum query on top of a union query? When I add date to the 2nd query, thus making each row distinct, I see all 4 records.
Take a step back. Change your first query :
Then
I think it all got too complicated with the layers of grouping.
EDIT re comment