Results 1 to 7 of 7
  1. #1
    dr4ke is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    108

    Aggregate and Non-Aggregate

    Afternoon,



    A, hopefully, easy question for a Friday afternoon!

    I know you can't use an aggregate and a non-aggregate function in a Query, however is there a workaround which would allow me to do the below?

    'Painting the picture'; There is a table with 4 columns. 'SanName', 'AggregateName', 'Used', 'Date'. For simplicity we'll say there are two combinations of these (SAN1 Aggregate1 & SAN1 Aggregate2), so to compare the data between the two dates I have to use the FIRST(), LAST() expressions. When I do this I can't then SELECT 'SanName' or 'AggregateName' due to the 'aggregate and non-aggregate' error.

    Is there a way to use the FIRST(), LAST() and SELECT expressions in the same query? So the output would be;

    SANName AggregateName FirstAmount LastAmount
    SAN1 Aggregate1 5000 6000
    SAN1 Aggregate2 5000 6000

    Any help appreciated

    Dr4ke

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,529
    Sure, you likely just need to add the GROUP BY clause:

    SELECT SanName, AggregateName, First(Amount) As FirstAmount, Last(Amount) As LastAmount
    FROM TableName
    GROUP BY SanName, AggregateName

    You may find first and last don't do what you expect:

    http://support.microsoft.com/kb/208190

    It sounds vaguely like you might be going in this direction:

    http://www.baldyweb.com/LastValue.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    dr4ke is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    108
    Thank you! The FIRST() LAST() now does exactly what I want it to do!

    I did look into the MIN() and MAX() way, but it would involve me creating 4 different queries to get the same output and then have to create another layer of queries to do Math on them. It seems as though the FIRST() and LAST() is the way to go! I just need to figure out how to implement the below; I can't seem to include a WHERE in this now due to the 'aggregate, non-aggregate' thing now. Any ideas? I have a Form, for the End User, where they select the date range they want to see the data between.

    WHERE ((HighLevelCapacity.Date) Between (Forms!ReportDataRangeSelect!SelectStart) And (Forms!ReportDataRangeSelect!SelectEnd))

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,529
    You should be able to do that. In design view, change Group By to Where on that field. In SQL view, make sure the date field is not in the SELECT clause or the GROUP BY clause.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    dr4ke is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    108
    Absolutley spot on!!

    I was putting the WHERE after the GROUP BY! I've learnt my lesson!! There doesn't appear to be any problems with my FIRST() LAST() Query not giving the desired outcome... Am I at risk?

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,529
    Excellent!

    You're not at risk if you've reviewed the link and understand what they are returning, which is often not what people expect.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    dr4ke is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    108
    Thank you

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

Similar Threads

  1. aggregate functions
    By gsrikanth in forum Access
    Replies: 3
    Last Post: 07-10-2012, 03:56 PM
  2. Aggregate query issue
    By TheShabz in forum Queries
    Replies: 8
    Last Post: 09-06-2011, 05:09 PM
  3. Aggregate Query
    By DonL in forum Queries
    Replies: 1
    Last Post: 08-04-2011, 09:54 AM
  4. Help in aggregate query
    By somm in forum Queries
    Replies: 1
    Last Post: 02-15-2011, 10:18 AM
  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