Solved

Aggregate Function error

Posted on 2016-10-09
7
35 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 19
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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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 19

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 19
ID: 41836927
you're welcome ~ happy to help
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

911 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

19 Experts available now in Live!

Get 1:1 Help Now