Solved

Aggregate Function error

Posted on 2016-10-09
7
30 Views
Last Modified: 2016-10-10
Experts, I am trying to add the below to a query and after doing so I get an error: "YOu tried to execute a query that does not include the specified expression 'Amount' as part of an aggregate function.

AccrueUSDEquiv: Sum(IIf([CurrencyID]=1,Nz([TAccrueMoINLocalCurr],0),Nz(([TAccrueMoINLocalCurr]*[ExchangeRate]),0)))

Why do I get this error?  The query works fine until I try to add this.
0
Comment
Question by:pdvsa
  • 3
  • 3
7 Comments
 

Author Comment

by:pdvsa
ID: 41835927
here is the query:

its big
SELECT
tblDraws_Details1.Amount, tblDraws_Details1.ValueDate, tblRepayment.Amount, tblRepayment.Currency, tblRepayment.ValueDate, tblRepayment.PaymentMadeYN, (DSum("Amount","tblDraws_Details1","ID = " & [DrawIDrpmt])+Nz(DSum("Amount","tblRePayment","DrawIDrpmt = " & [DrawIDrpmt] & " and PaymentMadeYN = 'Yes' "),0)) AS Balance, Sum(IIf([CurrencyID]=1,Nz([TAccrueMoINLocalCurr],0),Nz(([TAccrueMoINLocalCurr]*[ExchangeRate]),0))) AS AccrueUSDEquiv, Format([tblRepayment].[ValueDate],"mmmm") AS CurrentMonthToAccrue, ([Balance])*([SiborRate]+[Margin])*([DayCountToMaturity]/360) AS TInterestToMatLocalCur, ([tblRepayment].[ValueDate])-([tblDraws_Details1].[ValueDate]) AS DayCountToMaturity, DLookUp("[MarginRate]","tblInterest_Margin","[FacID]= " & [tblRepayment].[ID_Facility]) AS Margin, tblInterest_Sibor.SiborRate, ([Balance])*([SiborRate]+[Margin])*([DaysToAccrueCurMo]/360) AS [TAccrueCurrMo(InLocalCurr)], tblCurrencyExchange.CurrencyName, [tblRepayment].[ValueDate]-dhFirstDayInMonth([tblRepayment].[valuedate])+1 AS DaysToAccrueCurMo, tblRepayment.Comments
FROM
tblDraws_Details1 INNER JOIN ((tblRepayment LEFT JOIN tblInterest_Sibor ON tblRepayment.ID = tblInterest_Sibor.RpmtID) LEFT JOIN tblCurrencyExchange ON tblRepayment.Currency = tblCurrencyExchange.CurrencyID) ON tblDraws_Details1.ID = tblRepayment.DrawIDrpmt
WHERE
(((Format([tblRepayment].[ValueDate],"mmmm"))=Format(Date(),"mmmm")) AND ((tblDraws_Details1.ID_facility)=15))
ORDER BY
tblDraws_Details1.ID;
0
 
LVL 18
ID: 41835939
in order to use SUM, you must use an aggregate function on the rest of the columns such as GROUP BY

optionally, if you don't want to do that, you can use DSUM which needs the fieldname (or expression), table or query name, and conditions for matching
0
 

Author Comment

by:pdvsa
ID: 41835945
Hi Crystal,

thank you.  I didnt know I could do that.  I would like to use the Dsum method.  
I simply changed Sum to Dsum and i get a wrong number of arguments error.  
I am sure there is more to it than simply doing what i did.  

what should I do now?  grateful for your help.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 250 total points
ID: 41836025
Yes, but using domain functions in a query is generally considered to be a bad practice.  They are slow to execute, and because the query optimizer cannot use them, the query it generally poorly optimized as well.

I would recommend that you either write correlated sub-queries to get your sums, or write aggregate sub-queries and join them to the other tables in the query.
0
 
LVL 18

Assisted Solution

by:crystal (strive4peace) - Microsoft MVP, Access
crystal (strive4peace) - Microsoft MVP, Access earned 250 total points
ID: 41836036
you're welcome

as Dale suggested, a subquery might be better.  You can also make another query and join it in.

the problem using SUM is that you then must apply an aggregate to every column, which is not happening

> "domain functions in a query is generally considered to be a bad practice.  They are slow to execute"
on indexed fields, they actually perform quite well

> "wrong number of arguments error."
post your SQL.  You can also try a subquery ...
0
 

Author Closing Comment

by:pdvsa
ID: 41836331
Thank you very much.  I will work on crafting something.  Step by step.  

Have a good night
0
 
LVL 18
ID: 41836927
you're welcome ~ happy to help
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now