Results 1 to 9 of 9
  1. #1
    CT_AccessHelp is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    40

    Sum If Calculation Help

    Haven't used access in years so I'm a little rusty. Trying to query sum of a column based on text selection of another column.

    In the end I need Total Acres by Practice to equal Approved+Offered-Returned-Withdrawn CP23 would be 29 acres total in this example.



    Any help yall can give would be appreciated, Thanks!

    Practice Acres Status
    CP23 12 Withdrawn
    CP23 22 Approved
    CP23 32 Offered
    CP23 13 Returned
    CP40 24 Approved
    CP40 34 Offered

  2. #2
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    something like

    Code:
    SELECT Practice, sum(Acres*iif(status in ("approved","Offered"),1,-1)) as totalacres
    FROM myTable
    GROUP BY Practice
    Last edited by CJ_London; 03-03-2016 at 11:53 AM. Reason: missed a bit

  3. #3
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Use 2 queries.

    First, create a crosstab query on the table, with Practice as the row heading, status as the Column heading, and Acres as the values.

    Then create a second query with the first one as the source, and do the calculation there.

    I tried it with your data - it works fine.

  4. #4
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hey, Ajax - I like that a lot better - very elegant.

    J.

  5. #5
    CT_AccessHelp is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    40
    How would it work out if the table looked like the following? This is currently how the table is, I was maybe going to change it but if I can make it work the way they designed it I would like that as it will be a lot less work in the end.

    CP23 CP40 Status
    12 0 withdrawn
    22 0 approved
    32 0 offered
    13 0 returned
    0 24 approved
    0 34 offered

  6. #6
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    the way you have your table at the moment is not normalised - every time you add a new practice, you need to modify the table and all related queries (which will be much more complicated). Store the data as you showed it in your original post

  7. #7
    CT_AccessHelp is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    40
    I was afraid you'd say that - This thing already has all kinds of forms reports and queries tied to it so I was hoping to not reinvent the wheel.

  8. #8
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    the time you spend now normalising the data will save you endless hours modifying reports and forms in the future and having to maintain unnecessarily complex code.

  9. #9
    CT_AccessHelp is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    40
    Quote Originally Posted by Ajax View Post
    the time you spend now normalising the data will save you endless hours modifying reports and forms in the future and having to maintain unnecessarily complex code.
    Thank you! I have updated the table and things are working much better now.

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

Similar Threads

  1. mpg calculation
    By papavictorlima in forum Queries
    Replies: 3
    Last Post: 02-02-2016, 02:46 PM
  2. Help with a calculation
    By Cdray in forum Programming
    Replies: 9
    Last Post: 06-26-2015, 09:37 AM
  3. How to do this calculation
    By rickroller5 in forum Access
    Replies: 4
    Last Post: 09-07-2014, 01:22 PM
  4. Calculation Field in table vs Query calculation
    By Leonidsg in forum Database Design
    Replies: 18
    Last Post: 02-28-2013, 09:38 PM
  5. Need help with calculation please
    By Gabriel984 in forum Forms
    Replies: 6
    Last Post: 09-09-2010, 12: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