MS Access 2010 Query inside Query problem

The database I am working with has just 1 large table (which I know is ridiculous but 1 step at a time here). I need to produce a single query that displays 3 calculations. It seems easy enough but I am clearly missing something because I keep getting prompted with a "Enter Parameter" error. Currently, I have created 2 separate queries that calculate "Total Payment" and "Total Charge" now the final query is where I am having an issue. It displays the "Total Payment" and "Total Charge" correctly however I cannot get it to subtract the two and display in a third column named "Difference". Please let me know if any further info is required. I attached the SQL code. Thank you in advance!!!!
clover10Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dale FyeCommented:
What do the first 2 queries look like?
0
clover10Author Commented:
query 1:
SELECT DExtract.[Element], Sum(DExtract.[AmountTBCAPB]) AS [Total Charges]
FROM DExtract
GROUP BY DExtract.[Element];

Open in new window


query 2:
SELECT DExtract.[Element], DExtract.[ValTT], Sum(DExtract.[AmountTBCAPB]) AS [Total Payments]
FROM DExtract
GROUP BY DExtract.[Element], DExtract.[ValTT]
HAVING (((DExtract.[ValTT])="Payments"));

Open in new window

0
als315Commented:
Try this query:
SELECT DExtract.[Element], Sum(DExtract.[AmountTBCAPB]) AS [Total Charges] 
Sum(IIF(DExtract.[ValTT]="Payments", DExtract.[AmountTBCAPB], 0)) AS [Payments]  
Sum(IIF(DExtract.[ValTT]<>"Payments", DExtract.[AmountTBCAPB], 0)) AS [Difference] FROM DExtract
GROUP BY DExtract.[Element];

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

clover10Author Commented:
That makes sense to me.. it gives me a "the SELECT statement includes a reserved word or an argument that is mispelled or missing, or the punctuation is incorrect"  I am looking to see if I can find the error too
0
als315Commented:
Sorry, commas were skipped
SELECT DExtract.[Element], Sum(DExtract.[AmountTBCAPB]) AS [Total Charges], 
Sum(IIF(DExtract.[ValTT]="Payments", DExtract.[AmountTBCAPB], 0)) AS [Payments],  
Sum(IIF(DExtract.[ValTT]<>"Payments", DExtract.[AmountTBCAPB], 0)) AS [Difference] FROM DExtract
GROUP BY DExtract.[Element];

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
clover10Author Commented:
Thanks!! I think we're almost there.. I get an error now that says "Enter Parameter Value: DExtract.AmountTBCAPB"
0
als315Commented:
If you have table DExtract and field AmountTBCAPB in it, syntax seems correct now.
Can you upload sample DB with only few strings from your table (only this table, without other objects)?
0
als315Commented:
Sample DB
DbExtract.accdb
0
clover10Author Commented:
It is huge.. so here is a subset
DExtract-Sample.xlsx
0
clover10Author Commented:
I see your example makes perfect sense. I think the issue is there are multiple "AmountTBCAPB" lines per "Element". For the "Total Charges" I need everything in the "AmountTBCAPB" to add up per "Element". For the "Payments", I need everything in "AmountTBCAPB" added up where ValTT = "Payments".  The final difference column, Total Charges - Payments. Does that make sense?
0
als315Commented:
I see one Payment is positive - is it correct?
Can you show expected result for your sample?
Try new sample
DbExtract.accdb
0
clover10Author Commented:
Payment is not always positive, sometimes it is also negative
0
clover10Author Commented:
0
clover10Author Commented:
I got it now! Thanks so much!!!
0
clover10Author Commented:
Perfect!! Exactly what I needed and fast!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.