Results 1 to 5 of 5
  1. #1
    vitordf is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    9

    Cannot Have Aggregate function

    Hi,



    I am having a problem when I try to run an "Update" query, if I run the query with:

    Code:
    WHERE ((Tbl_EmployeeAll.LastOfDept)="Service")
    The same runs OK, but when I try to run with
    Code:
    WHERE (((Tbl_EmployeeAll.LastOfDept)="Service") AND ((Tbl_Offers_Raw.Week)=Max([Tbl_Offers_Raw.Week])))
    Then it gives me the error message:
    Click image for larger version. 

Name:	accesserror.jpg 
Views:	8 
Size:	23.5 KB 
ID:	13418

    Any idea what am I doing wrong...

    Thanks in advance,

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    If you are using an Aggregate Function in your criteria, it needs to be in the HAVING clause, not the WHERE clause.
    See: http://office.microsoft.com/en-us/ac...001032238.aspx

  3. #3
    vitordf is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    9
    Quote Originally Posted by JoeM View Post
    If you are using an Aggregate Function in your criteria, it needs to be in the HAVING clause, not the WHERE clause.
    See: http://office.microsoft.com/en-us/ac...001032238.aspx
    Thanks for the information but I don't see the aggregate so I am confused, below is the full SQL, can you perhaps check it if possible and let me now where I am wrong

    Code:
    INSERT INTO Tbl_OffersComplete_TEST 
    ( Week, Offer_Code, Description, UserID, EmployeeName, [Manager Level 4 Id], [Manager Level 3 Id], [Manager Level 2 Id], [Manager Level 1 Id], Location, Region, Event_Type, Nbr_Offers, SkillGroup )
    
    
    SELECT 
    Tbl_Offers_Raw.Week, 
    Tbl_Offers_Raw.Offer_Code, 
    Tbl_Offers_Raw.Description, 
    Tbl_Offers_Raw.UserID, 
    Tbl_EmployeeAll.EmployeeName, 
    Tbl_EmployeeAll.[Manager Level 4 Id],
    Tbl_EmployeeAll.[Manager Level 3 Id], 
    Tbl_EmployeeAll.[Manager Level 2 Id], 
    Tbl_EmployeeAll.[Manager Level 1 Id], 
    Tbl_EmployeeAll.Location, 
    Tbl_Centres.Region, 
    Tbl_Offers_Raw.Event_Type, 
    Tbl_Offers_Raw.Nbr_Offers, 
    Tbl_SKGDetail.SkillGroup
    
    
    FROM
    ((Tbl_Offers_Raw INNER JOIN Tbl_EmployeeAll ON Tbl_Offers_Raw.UserID = Tbl_EmployeeAll.UserID) INNER JOIN 
    Tbl_SKGDetail ON Tbl_Offers_Raw.UserID = Tbl_SKGDetail.UserID) INNER JOIN 
    Tbl_Centres ON Tbl_EmployeeAll.Location = Tbl_Centres.OrgLocation
    
    
    WHERE ((Tbl_EmployeeAll.LastOfDept)="Service")
    HAVING ((Tbl_Offers_Raw.Week)=Max([Tbl_Offers_Raw.Week]));
    My aim is to only insert the records from the latest week within the Tbl_Offers_RAW.

    Thanks again,

  4. #4
    kazaccess is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Jul 2013
    Posts
    43
    You could also include a Select statement in the second where condition:
    WHERE (((Tbl_EmployeeAll.LastOfDept)="Service") AND [Tbl_Officers_Raw.Week] = (Select Max([Tbl_Offers_Raw.Week]) from TableName)

  5. #5
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    My aim is to only insert the records from the latest week within the Tbl_Offers_RAW.
    I don't think you can use MAX in your criteria if it is not anywhere in your Select statement.

    So you will either need to use a nested query (like kazaccess showed), or do it in a series of two queries (the first one to find the MAX value).

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 11-19-2012, 01:44 PM
  2. more aggregate function questions
    By boutwater in forum Access
    Replies: 6
    Last Post: 09-29-2011, 02:53 PM
  3. aggregate error due to function
    By boutwater in forum Access
    Replies: 2
    Last Post: 09-26-2011, 03:39 PM
  4. an aggregate function error message
    By newtoAccess in forum Queries
    Replies: 1
    Last Post: 11-27-2010, 05:18 PM
  5. Access SQL Query - Aggregate function
    By jack in forum Access
    Replies: 0
    Last Post: 11-10-2009, 08:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums