Results 1 to 9 of 9
  1. #1
    TheShabz is offline Court Jester
    Windows XP Access 2003
    Join Date
    Feb 2010
    Posts
    1,368

    Aggregate query issue


    Hi all,

    I have a query to the tune of:
    SELECT stuff
    FROM tablesWithJoins
    GROUP BY EverythingInSelect

    This works fine. Now I add:
    HAVING table1.DateField Between [table2].[DateField] And DateAdd("d",60,[table2].[DateField])

    I'm getting the "You tried to execute a query that does not included the specified expression (my HAVING clause) as part of an aggregate function" error. This query worked fine in 2003 but we recently upgraded to 2007 and now it's not working. What's changed between 2003 and 2007?

  2. #2
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    Try creating a query that does the Date filter.
    Then create a query on the above query and do your grouping in the second query.

    I'm not sure this is the most elegant solution - but it should work.

  3. #3
    TheShabz is offline Court Jester
    Windows XP Access 2003
    Join Date
    Feb 2010
    Posts
    1,368
    That should work as a patchjob but this query usually takes about 5 mins to run. it's hitting 2 linked tables with 10m+ records in each. Getting just the dates then querying again will just about double the process. I need to get this in one go.

    I just don't see why my Having clause is causing that issue.

  4. #4
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    I've had the same issue in Access 2010 - but I don't have near as many rows of data to worry about.

    BUT - I don't think doing it my way will increase or decrease the time the query takes to run. I could be wrong - but either way Access is going to have to Filter AND Group . . .

    I'd like to know if you find a more efficient way. I could use it myself.

  5. #5
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    This is a shot in the dark, but does this work (adding the expression to both the SELECT and GROUP BY clauses)?

    SELECT stuff, DateAdd("d",60,[table2].[DateField]) AS WhoCares
    FROM tablesWithJoins
    GROUP BY EverythingInSelect, DateAdd("d",60,[table2].[DateField])
    HAVING table1.DateField Between [table2].[DateField] And DateAdd("d",60,[table2].[DateField])
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    TheShabz is offline Court Jester
    Windows XP Access 2003
    Join Date
    Feb 2010
    Posts
    1,368
    Does not work.

  7. #7
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    Swing-and-a-miss = strike one.

    If all the fields are in the GROUP BY clause, does taking that away and making the HAVING clause a WHERE clause work? Perhaps with the DISTINCT keyword if appropriate?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    TheShabz is offline Court Jester
    Windows XP Access 2003
    Join Date
    Feb 2010
    Posts
    1,368
    SELECT DISTINCT Stuff
    FROM tablesWIthJoins
    WHERE table1.DateField Between [table2].[DateField] And DateAdd("d",60,[table2].[DateField])
    is running. I'll know soon whether Distinct gets all the dupes out.

  9. #9
    TheShabz is offline Court Jester
    Windows XP Access 2003
    Join Date
    Feb 2010
    Posts
    1,368
    scrapped the whole thing. Gonna do dupe checking afterwards. Maybe one day we can have non-aggregate criteria in an aggregate query. =/

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

Similar Threads

  1. Aggregate Query
    By DonL in forum Queries
    Replies: 1
    Last Post: 08-04-2011, 09:54 AM
  2. Help in aggregate query
    By somm in forum Queries
    Replies: 1
    Last Post: 02-15-2011, 10:18 AM
  3. Aggregate Query Returns No Values
    By Xiaoding in forum Queries
    Replies: 6
    Last Post: 03-29-2010, 02:01 PM
  4. Access SQL Query - Aggregate function
    By jack in forum Access
    Replies: 0
    Last Post: 11-10-2009, 08:06 PM
  5. Using an Aggregate in a query
    By jbh02 in forum Queries
    Replies: 0
    Last Post: 09-15-2009, 07:29 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