473,395 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Aggregate Function Error Message

Hello,

I've been getting the following error message in multiple queries, over and over again and I don't understand what it doesn't like.

"You tried to execute a query that doesn't include the specific expression '1*fCalculateMedian(tbl.MWHRSday.FLEET_ID,[avg_mwhrs])' as part of an aggregate function."

The '1*fCalculateMedian(tbl.MWHRSday.FLETT_ID,[avg_mwhrs])' part is always something different depending on which field it has issue with.

Anybody run into this before?
Sep 5 '08 #1
4 2098
missinglinq
3,532 Expert 2GB
This type of query is not my forte, but I'm sure someone else will be along to help you out! It's very good that you posted the error message you're receiving, but I'm pretty sure that you need to also post a sample of the SQL code that causing the problem, as well.

Linq ;0)>
Sep 5 '08 #2
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT tbl_MWHRSday.FLEET_ID, 1*(fCalculateMedian([FLEET_ID],[avg_mwhrs])) AS Median INTO tbl_fleetmedian
  2. FROM tbl_MWHRSday
  3. GROUP BY tbl_MWHRSday.FLEET_ID;

Also, here is the module fCalculateMedian

Expand|Select|Wrap|Line Numbers
  1. Public Function fCalculateMedian(strfleetname As String, dblmotorMWHRS As Double)
  2. Dim MyDB As DAO.Database, MyRS As DAO.Recordset, MySQL As String
  3. Dim intNumOfRecords As Integer, curMotorMWHR As Double
  4.  
  5. MySQL = "SELECT tbl_MWHRSday.FLEET_ID, tbl_MWHRSday.avg_mwhrs FROM tbl_MWHRSday "
  6. MySQL = MySQL & "WHERE tbl_MWHRSday.Fleet_ID='" & strfleetname & "' ORDER BY tbl_MWHRSday.Fleet_ID, tbl_MWHRSday.avg_mwhrs;"
  7.  
  8. Set MyDB = CurrentDb()
  9. Set MyRS = MyDB.OpenRecordset(MySQL, dbOpenSnapshot)
  10.  
  11. MyRS.MoveLast: MyRS.MoveFirst
  12.  
  13. intNumOfRecords = MyRS.RecordCount
  14. If intNumOfRecords = 0 Then Exit Function
  15.  
  16. If intNumOfRecords Mod 2 = 0 Then     'Even number of Records
  17.   MyRS.Move (intNumOfRecords \ 2) - 1   'Move half-way point
  18.     curMotorMWHR = MyRS![avg_mwhrs]      '1st value to average
  19.   MyRS.MoveNext
  20.     curMotorMWHR = curMotorMWHR + MyRS![avg_mwhrs]               '2nd value to average added to 1st value
  21.     fCalculateMedian = curMotorMWHR / 2  'Average them out
  22. Else   'Odd number of Records
  23.   MyRS.Move (intNumOfRecords \ 2)
  24.   fCalculateMedian = MyRS![avg_mwhrs]
  25. End If
  26.  
  27. MyRS.Close
Sep 5 '08 #3
NeoPa
32,556 Expert Mod 16PB
When dealing with GROUPed queries (where a GROUP BY clause exists) only items which are either themselves GROUPed BY, or are aggregated, can be displayed.

Aggregation is selecting a value which somehow represents the whole group of that item.

Aggregate functions are listed in the "Total" row in the Query Definition grid after selecting the Sigma button. This includes Sum(), Max(), Min(), First(), Last() etc.

It also includes Group. This means that the item is used to determine which records should be treated as together in a group.

A grouped item doesn't change in the group, so logically can be used directly in the SELECT clause.

All other items do, so must include something to indicate what should be used. Even an Expression must consist of only grouped or aggregated items.

Your problem is that you're trying to refer to a field, within a grouped query, which has no logical meaning.

Consider :
Table Name=[tblData]
Expand|Select|Wrap|Line Numbers
  1. Field        Type
  2. Class        String
  3. StudentName  String
  4. ExamScore    Number
Expand|Select|Wrap|Line Numbers
  1. Class  StudentName  ExamScore
  2. Maths  Andrew           55
  3. Maths  Brian            60
  4. Maths  Charles          65
  5. Maths  David            70
  6. Maths  Edward           75
  7. Maths  Francis          80
  8. Maths  Gerald           85
Expand|Select|Wrap|Line Numbers
  1. SELECT [Class]
  2.        Sum([ExamScore]) AS SumExams
  3.  
  4. FROM tblData
Result
Expand|Select|Wrap|Line Numbers
  1. Class  SumExams
  2. Maths     490
This illustrates both types of acceptable forms in a GROUP BY query.

Now consider how much sense there would be within this query to try to show the [StudentName]?

On its own it would have no meaning within the context. It would be possible (though hard to see the use) to use First([StudentName]).
Sep 6 '08 #4
FishVal
2,653 Expert 2GB
Hello, denveromlp.

First:
Why would you ever need to use GROUP BY clause in the query?
Second:
What for are you still using dblmotorMWHRS argument in fCalculateMedian() function?

Regards,
Fish
Sep 7 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: sausage31 | last post by:
I have a table as follows.... Device LotID Result1 Result2 Result3 aaa 1 5 10 15 bbb 1 2 4 6 aaa 2 ...
5
by: Jim | last post by:
Need help with aggregate function...for each unique product, I need the provider with the cheapest cost factor Here't the table (Table1) ID product provider factor 1 123456 abc...
2
by: deko | last post by:
SELECT Nz(Min(),#1/1/1000#) AS NextAppt, Appt_ID FROM tblAppointments WHERE Entity_ID=!! AND ApptDate>=Date() If I do not include Appt_ID, it works fine. But I need the ID so I can join other...
1
by: Najib Abi Fadel | last post by:
Hi i have an ordered table of dates let's say: 1/1/2004 8/1/2004 15/1/2004 29/1/2004 5/2/2004 12/2/2004
1
by: R.A.M. | last post by:
Hello, I am learning SQL Server 2005. I have (correctly) written in .NET assembly DemoSQLServer with aggregate function AvgNoMinMax in class Demo and I have added assembly to database...
5
by: BillCo | last post by:
I just wasted a long time figuring out this and I figure if I post it might save someone some pain! Jet (DAO) will allow you to to use nested aggregate functions like building blocks, e.g.: ...
3
by: Aaron | last post by:
I have been searching the boards trying to find an answer to this question and no luck. I am using a query similar to this: Select count(col1) from table1 I was having a hard time accessing...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
3
by: ncsthbell | last post by:
I am pulling my hair out on this! Seems like it should be easy, I just can not get it to work like I want. I know I am doing something wrong, so I hope someone can be so kind to guide me!!! I...
7
by: kpfunf | last post by:
Getting the following error opening a report, cannot figure out the cause: "You tried to execute a query that does not include the specified expression 'RQ_FuelQuoteReportHistory.Vendor' as part...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.