Results 1 to 8 of 8
  1. #1
    Fabdav is offline Novice
    Windows XP Access 2003
    Join Date
    Sep 2011
    Posts
    22

    Total positive and negative values in a query

    I have a table with all the sales
    I would like to have on two fileds the total of the sales and the total of the returns.


    At the moment I have to create two separte query one with values > 0 and one with values < 0.
    Can I do it on a sigle query?

  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,521
    Probably:

    SELECT Sum(IIf(Amount > 0, Amount, 0)) AS TotalPositive, Sum(IIf(Amount < 0, Amount, 0)) AS TotalNegative
    FROM TableName
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    swat is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    21
    Hi guys, I am having a similar issue as this one. I would like to have 1 query do the work of 3 if possible. Having 3 columns with 3 sums involving the same table but different criteria. I tried to incorporate Pbaldy's code and recieved this error, same error if I do it in design view. Here is the error.

    This expression is typed incorrectly, or it is too complex to be evaluated. For example, a numeric expression may contain too many complicated elements. Try simplifying the expression by assigning parts of the expression to variables. (Error 3071)

    Here is the SQL:

    SELECT Sum([Weight_Before]-[Weight_After]) AS Gas_Used, Sum(IIf(([SF6_Usage_Log].[Initial_Fill]=Yes),([Weight_Before]-[Weight_After]),0)) AS Initial_Gas, Sum(IIf(([SF6_Usage_Log].[Initial_Fill]="No"),([Weight_Before]-[Weight_After]),0)) AS Transient_Gas
    FROM SF6_Usage_Log
    HAVING (((SF6_Usage_Log.Date_Used) Like "*" & [What year? (yyyy)]));

    I have also tried design view in the "Field" box:

    Initial_Gas: [Weight_Before]-[Weight_After] AND [Initial_Fill]="Yes"
    and the "Total" box: Sum

    And recieved the same error.

    Any help would be greatly appreciated.

    Shawn
    Last edited by swat; 09-16-2011 at 12:30 PM. Reason: spelling

  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,521
    What is the data type of Initial_Fill? You're treating it 2 different ways. Also, if Date_Used is a date field, I don't know how reliable using Like will be. Try:

    HAVING Year(SF6_Usage_Log.Date_Used) = [What year? (yyyy)]
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    swat is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    21
    Thank you for the quick reply pbaldy. The Initial_Fill column is a "Yes/No" box. Also, if the Date_Used field is a mm/dd/yyyy date, will the Having Year() function still work?

  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,521
    Then "No" should be No (or 0).

    If the data type is date/time, the year function will work regardless of format.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    swat is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    21
    Awesome, I understand now what you were saying. I changed the date code to match yours as well. It works as intended now. Thank you so much.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Converting Positive to Negative
    By TheProfessorIII in forum Access
    Replies: 5
    Last Post: 03-22-2011, 06:38 AM
  2. Summing Positive and Negative Currency
    By nweird in forum Reports
    Replies: 1
    Last Post: 07-22-2010, 10:05 AM
  3. Replies: 2
    Last Post: 01-13-2010, 05:29 AM
  4. Convert Negative Values to Positive
    By creativefusion in forum Queries
    Replies: 1
    Last Post: 10-21-2009, 02:47 AM
  5. Matching positive with negative amounts
    By cwert11 in forum Access
    Replies: 1
    Last Post: 09-29-2008, 12:26 AM

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