Results 1 to 4 of 4
  1. #1
    DBS1 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2020
    Posts
    47

    Calculating IIf statement using two fields

    I have a query where I'm trying to create a field that calculates based off of another field. I'm trying to create an IIf statement that would give me the Cumulative field based on the Segment field, i.e., If Segment=B, Sum A and B, and if Segment=C, Sum A, B, and C, etc. Appreciate if someone could point me in the right direction. Thanks.

    *Desired field*
    Area Segment Total Cumulative
    East A 5 5
    East B 7 12


    East C 2 14
    East D 14 28
    West A 16 16
    West B 9 25
    West C 10 35
    West D 7 42

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,445
    the problem with very simplistic data is it often does not reflect reality so having got a simplistic solution you then find it does not meet reality.

    But to meet your data, something like this

    SELECT A.Area, A.Segment, A.Total, sum(B.Total) as Cumulative
    FROM myTable A INNER JOIN myTable B ON A.area=B.area and B.segment<=A.segment
    GROUP BY A.Area, A.Segment, A.Total
    ORDER BY A.Area, A.Segment
    Note this can only be done in SQL, you cannot user the query GUI

  3. #3
    DBS1 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2020
    Posts
    47
    Ajax - thanks for your quick response. One thing I forgot to mention is the source of the data is from qrySiteVisits. Also, what I'm trying to achieve is for Segment A in Area East to return the sum of B, C, and D for East, and the same for West, and for Segment B to return the sum of C and D and for C to return the amount for D. (I also have North and South but didn't include those originally for simplicity) If you could give me a sample code for this, it would be greatly appreciated.

  4. #4
    DBS1 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2020
    Posts
    47
    Shortly after I posted the last message, I was able to resolve my query in a round about way. I used another query that calculated the fields I was looking for by using IIf statements, then used IIf statements to meet the criteria of the segment A, B, and C fields. Not something I'd suggest for anyone to use, but it accomplished my goal for the report I need. Didn't want to waste anyone's time since I've found a solution.

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

Similar Threads

  1. Query not calculating all fields
    By koncreat in forum Queries
    Replies: 2
    Last Post: 09-27-2018, 02:50 PM
  2. Replies: 18
    Last Post: 02-06-2013, 12:43 PM
  3. Calculating subform fields
    By Tvanduzee in forum Forms
    Replies: 25
    Last Post: 09-17-2012, 06:42 AM
  4. Replies: 0
    Last Post: 03-09-2012, 07:04 AM
  5. calculating time fields
    By donnan33 in forum Programming
    Replies: 5
    Last Post: 01-19-2012, 12:15 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