Results 1 to 5 of 5
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    2,073

    Missing Something Using One of the Aggregate Functions (SUM)

    Doing a little debugging trying to understand what I'm missing here: (Just wanting to sum up the "Shares" field for a subset (InvstID=2) of table Ledgers)
    Click image for larger version. 

Name:	Aggregate.jpg 
Views:	10 
Size:	79.9 KB 
ID:	19113

    Is there simply a syntax error in the way I coded the SUM function?

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    In order to use an Aggregate Function like "Sum", it needs to be an Aggregate Query. In an Aggregate Query, every field being returned must either be "Grouped" or have an Aggregate Function applied to it. Your InvstID field has neither.

    Here is how you want to do this Query:
    1. Add the InvstID and Shares to a query
    2. Click on the "Totals" button, which looks like a Sigma or Summation sign.
    3. This will add a "Totals" row under each field. Leave InvstID as "Group By", but change Shares to "Sum"
    4. Add your Criteria to the InvstID field (=2)
    5. View your results.

  3. #3
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    2,073
    Thanks, I was way off base now seeing what the SQL looks like:

    Code:
    SELECT Ledgers.InvstID, Sum(Ledgers.Shares) AS SumOfShares
    FROM Ledgers
    GROUP BY Ledgers.InvstID
    HAVING (((Ledgers.InvstID)=2));
    It's easier for me to use design mode like you've shown me with this post to get the SQL expression that I can then use as the ControlSource of a text box. (Actually, I have to create the query in code where I can change the value of InvstID and then set the ControlSource accordingly.)

    Thanks,
    Bill

  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,641
    Hi Bill, sounds like you might get a white Christmas?

    Very minor point. That query would be slightly more efficient as:

    SELECT Ledgers.InvstID, Sum(Ledgers.Shares) AS SumOfShares
    FROM Ledgers
    WHERE Ledgers.InvstID=2
    GROUP BY Ledgers.InvstID

    in other words, using a WHERE clause instead of a HAVING clause. I know, minor point, mainly I just wanted to say hi.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 7 64bit Access 2003
    Join Date
    Feb 2011
    Posts
    2,073
    Hey Paul, and a Merry Christmas to you!

    What really needs to happen is WHERE Ledgers.Invst = Portfolio.Invst

    See my new post where I'm attempting to use the query as the ControlSource of an unbound text box. Maybe not the right approach?

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

Similar Threads

  1. Replies: 8
    Last Post: 03-26-2014, 02:23 PM
  2. Including aggregate functions in a query
    By frind in forum Queries
    Replies: 2
    Last Post: 04-19-2013, 11:50 AM
  3. Finding Max of two nested aggregate functions in SQL
    By SummertimeClothes in forum Queries
    Replies: 1
    Last Post: 10-30-2012, 12:00 PM
  4. aggregate functions
    By gsrikanth in forum Access
    Replies: 3
    Last Post: 07-10-2012, 03:56 PM
  5. Replies: 5
    Last Post: 02-23-2012, 05:22 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