473,379 Members | 1,312 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,379 software developers and data experts.

How do I write a query expression with MAX?

Below is the SQL statement.
I am trying to group by the TICKET_NO and grab the MAX of the MAX_DATE field (which is the STATUS_DATE AND STATUS_TIME combined)

When I run the query, it keeps returning: "You tried to execute a query that does not include the specified expression 'CREATE_DATE' as part of an aggregate function."

Any help is much appreciated.


Expand|Select|Wrap|Line Numbers
  1. SELECT tblIncident.TICKET_NO, tblIncident.CREATE_DATE, tblStatusHistory.STATUS_DATE, tblStatusHistory.STATUS_TIME, Max([STATUS_DATE] & " " & [STATUS_TIME]) AS MAX_DATE
  2. FROM (tblIncident LEFT JOIN tblStatusHistory ON tblIncident.IncidentID = tblStatusHistory.IncidentID) INNER JOIN tblStatus ON tblIncident.STATUS = tblStatus.STATUS
  3. WHERE (((tblStatus.STATUS)='Active'))
  4. GROUP BY tblIncident.TICKET_NO;
  5.  
Oct 21 '11 #1

✓ answered by NeoPa

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 compound references, must either be aggregated (included in one of the aggregate functions - Sum(), Avg(), Min(), Max(), Count(), StDev(), Var(), First() or Last()) 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.

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 many possible 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

2 3784
patjones
931 Expert 512MB
When you use GROUP BY, everything that you put in the SELECT clause must either 1) be included in the GROUP BY clause, or 2) be an aggregate function (MAX, MIN, AVG, etc.).

In your situation, CREATE_DATE, STATUS_DATE, and STATUS_TIME are not part of the GROUP BY, and so the system is complaining about that. If you remove those columns, and leave just TICKET_NO and MAX_DATE, the query should run (barring some other problem that I'm not seeing with the JOIN).

Pat
Oct 21 '11 #2
NeoPa
32,556 Expert Mod 16PB
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 compound references, must either be aggregated (included in one of the aggregate functions - Sum(), Avg(), Min(), Max(), Count(), StDev(), Var(), First() or Last()) 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.

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 many possible 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
Oct 21 '11 #3

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

Similar topics

29
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
4
by: dcarson | last post by:
I've read about this error in several other discussions, but still can't seem to pinpoint the problem with my code. Everything seemed to be working fine for some time, but it now tends to bomb out...
2
by: Yisroel Markov | last post by:
Greetings, In Access 2000, I have a select query where I need to write an expression that will look at a text field and return the third character. (This will be used later in a report based on...
2
by: JimFor | last post by:
Hi, Is there any way in Access 2002 to copy a query expression from a query which was already set up to one which is being made? This would avoid having to enter a long equation more than once....
1
by: Tooled | last post by:
I am writing a vb.net app that uses ms-access as its database. I need to allow users to create their own custom sql expressions that will eventually be used in their reports. After they have...
1
by: amitbadgi | last post by:
I am geting the following error while conv an asp to asp.net Exception Details: System.Runtime.InteropServices.COMException: Syntax error in query expression 'id =System.__ComObject'. Source...
10
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp "If the value of the variant argument is Null, the Nz function returns the number zero or a...
9
by: Alan Mailer | last post by:
Ok, my Access 2002 language writing skills are VERY rusty,. I would know how to do what I need using SQL Server's "Coalesce' function, but I don't have that available to me in the Access 2002...
5
by: elie.constantine | last post by:
Hello A person developed a Microsoft Access Solution and gave me the MDE file. When I tried to run it, I got "Function is not available in expressions in query expression 'Trim([......" Error. ...
5
by: Just_a_fan | last post by:
I tried to put an "on error" statement in a routine and got the message that I cannot user "on error" and a lamda or query expression in the same routine. Help does not list anything useful for...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...

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.