473,882 Members | 1,646 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Aggregate Query Woes

NeoPa
32,584 Recognized Expert Moderator MVP
Introduction :

How many times have you come across the following error message when working within Aggregate, or Sum, queries?
Access Error:
You tried to execute a query that does not include the specified expression 'X' as part of an aggregate function.
What is this message relating to and how can one avoid it?

Aggregate (Sum) Functions :

These are the standard functions available to Jet SQL. I can't say no others can exist, nor that they may not be introduced later into Jet SQL itself, but these are currently available :
Sum(), Avg(), Min(), Max(), Count(), StDev(), Var(), First() and Last()

Converting a Query to Aggregate (or Sum) :

In SQL the GROUP BY clause can be used, but is actually not absolutely necessary. Any reference to an aggregate function within the SQL will cause the query to behave as an aggregate one. If no GROUP BY clause is included the whole dataset is treated as a single group.

In Design View select View | Totals, or click on the Sigma-shaped button (Like an 'M' rotated 90 degrees anti-clockwise), to change the options to allow selecting of aggregate functions and also fields to group by.

Access Help :

It may help to start with what Access Help has to say on the subject :
Jet SQL Help:
All fields in the SELECT field list must either be included in the GROUP BY clause or be included as arguments to an SQL aggregate function.
This quote from the Help system implies that all references to fields, even within compound references, must either be aggregated (IE. included in one of the aggregate functions listed above) or included in the GROUP BY clause. Any expression which is of either type is considered aggregated. The aggregate functions can only take field reference expressions which resolve to non-aggregated fields (IE. It is equally invalid to aggregate data more than once).

Explanation and Example Illustration :

Essentially, the SQL Engine needs to know how to process any particular reference. If you have a reference to a field which is not inside an aggregate function of any type, and is also not in the GROUP BY clause, then how is the SQL Engine to know which of the potentially many records in the group to take the value from.

See the situation below and ask yourself whether the result for [fB] should be "A", "B" or "C" :
Expand|Select|Wrap|Line Numbers
  1. SELECT   [fA]
  2.        , [fB]
  3.        , Sum([fC]) AS [SumC]
  4. FROM     [Tbl]
  5. GROUP BY [fA]
Expand|Select|Wrap|Line Numbers
  1. Table = [Tbl]
  2. [fA]  [fB]  [fC]
  3.  1    "A"    11
  4.  1    "B"    22
  5.  1    "C"    33
Result :
Expand|Select|Wrap|Line Numbers
  1. [fA]=1, [fB]="???", [SumC]=66
Conclusion :

The fact that you cannot determine from this information which item should be selected, is an illustration of why it doesn't make sense unless the above criteria are met (In short that all field references are aggregated in one way or another).
Mar 19 '12 #1
0 18775

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

Similar topics

2
3442
by: Toby Dann | last post by:
I have an aggregate query as recordsource for a form to show a list of invoices, including the totals (calculated by the aggregate query - I'm trying to duplicate as little info as possible here). What I want to achieve is a recordcount (of the number of records returned by the aggregate query) and Sum of the Invoice totals, but Access does not seem to like calculating either of these on an aggregate query. Any ideas? Am I going about...
1
2265
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 3 6 9 I need to determine the mean of all results for each LotID, so used the Row Average function described on the Microsoft Knowledge base, which gives
10
11943
by: neb | last post by:
Dear member of the forum, Ms access has built-in aggregate function like: -Sum, Max, First, Avg, ... Is it possible to build user-defined aggregate? (if you have any clue, do not hesitate to post) *I'm running a total query, of the form
1
3174
by: Scott Gerhardt | last post by:
Hello, I am new to the list, my apology if this question is beyond the scope or charter of this list. My questions is: What is the best method to perform an aggregate query to calculate sum() values for each distinct wid as in the example below, but except for all wid's (not just WHERE wid='01/1-6-1-30w1/0'). Also, performance wise, would it be better to build a function for this query. The table has 9 million records and these...
2
1501
by: lrgclasper | last post by:
Greetings, I have the following select statement from a single table ( Haul number, weight, species code). I would like the output to provide me with hauls with only two species. When I use an OR clause it returns haul specific records for each species, some hauls have both species (which is what I want) and some hauls have only one species. So, basically I am looking to have an extrapolated weight for each haul having two different...
3
1495
by: brent78 | last post by:
I'm trying to create a query that finds the max of a group of records and then displays all details about that record. Suppose this is my data: Type Size Color Quantity A L Green 5 A M Blue 7 B S Orange 2 B M Red 4 B L Purple 5 C M Red 10 C L Green 12
3
1966
by: John | last post by:
Is there a way to create a grouping query and be allowed to edit the data. I want to create a query that has a grouping and totals but also allow the user to edit. Can it be done?
1
1768
by: lorirobn | last post by:
Hi, I have a report that displays summary information, summing prices for all records for a RoomID meeting certain criteria, and printing the roomID and sum on a detail line. Now I want to add lookup-table info onto the detail line. The report's query determines what items are missing for a Room's Design Type, and it's pretty complicated for me, using an EXISTS and matching on NULLS.
4
1796
by: sbowden81 | last post by:
Hi all, I'm trying to create what I thought was a simple inventory query, but I'm having a problem. I have a shipment table that looks like this: SHIP_ID SHIP_QTY 1 24 2 12 Then a table that shows when certain items from a shipment are "processed":
1
1844
by: clickingwires | last post by:
How do you consecutively number rows in an aggregate query?
0
9931
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9777
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11108
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10830
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10403
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9557
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5781
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4601
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.