Solved

Aggregate Function error

Posted on 2016-10-09
7
41 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 21
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
Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

 
LVL 48

Accepted Solution

by:
Dale Fye 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 21

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

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

636 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