Having not working in an aggregate query

Pat
5StarLounger
Posts: 1148
Joined: 08 Feb 2010, 21:27

Having not working in an aggregate query

Post by Pat »

My query is:
SELECT [tbl First NN Trades].User, [tbl First NN Trades].Trader, [tbl Trades at a Time Summary].Noof, Sum([tbl First NN Trades].Pips) AS SumOfPips, Sum([tbl First NN Trades].Profit) AS SumOfProfit, DateValue(Min([DateClose])) AS MinDate, DateValue(Max([DateClose])) AS MaxDate, [SumofProfit]/[forms]![frm Import]![TradesAtaTimeMonths] AS AvgPerMth, [AvgPerMth]*12 AS AnnualForecast
FROM [tbl First NN Trades] INNER JOIN [tbl Trades at a Time Summary] ON [tbl First NN Trades].Trader=[tbl Trades at a Time Summary].Trader
WHERE ((([tbl First NN Trades].User)="Providers")) OR ((([tbl First NN Trades].User)="Providers"))
GROUP BY [tbl First NN Trades].User, [tbl First NN Trades].Trader, [tbl Trades at a Time Summary].Noof
HAVING (((Sum([tbl First NN Trades].Profit)/forms![frm Import]!TradesAtaTimeMonths)>forms![frm Import]!ExclProfLT)) Or (((forms![frm Import]!ExclProfLT) Is Null))
ORDER BY Sum([tbl First NN Trades].Profit) DESC;

The WHERE clause should show for User = "Providers", why it shows twice I don't know.

The control forms![frm Import]!ExclProfLTcontains 1000 yet the Average per month still shows for records less than 1000.

User avatar
HansV
Administrator
Posts: 78620
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Having not working in an aggregate query

Post by HansV »

The condition for User is probably included twice because Access splits the HAVING condition into two rows. It isn't needed twice, however. You can change it to

WHERE ((([tbl First NN Trades].User)="Providers"))

You should express the HAVING condtion in terms of Sum([tbl First NN Trades].Profit) instead of Sum([tbl First NN Trades].Profit)/[forms]![frm Import]![TradesAtaTimeMonths]:

HAVING (((Sum([tbl First NN Trades].Profit))>[forms]![frm Import]![TradesAtaTimeMonths]*[forms]![frm Import]![ExclProfLT])) OR ((([forms]![frm Import]![ExclProfLT]) Is Null))
Best wishes,
Hans

Pat
5StarLounger
Posts: 1148
Joined: 08 Feb 2010, 21:27

Re: Having not working in an aggregate query

Post by Pat »

Thanks Hans, that did the trick. Petty or pedantic animal at times is our Access

User avatar
HansV
Administrator
Posts: 78620
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Having not working in an aggregate query

Post by HansV »

HAVING is intended to be used with aggregate functions such as Sum, Max etc.
Using it with an expression that involves aggregate functions makes it too complicated to process.
Best wishes,
Hans