Results 1 to 7 of 7
  1. #1
    CubsFan is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2020
    Posts
    4

    Crosstab query with calculation on two (or more) fields?

    How do I perform a calculation on the values of two fields (or more) in a crosstab query? Sample table contains: [State], [County], [CountofTrials], [CountofSuccessful_Trials], [ActivityDate]. I need to create a crosstab query with row = State or County; column = ActivityDate (grouped by month); value = ratio of Successful Trials/Trials. I can create a crosstab to give me trials and a crosstab to give me successful trials, but I cannot figure out how to get a crosstab to calculate the ratio of success trials to trials.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    So divide CountofSuccessful_Trials by CountofTrials and that will be the Value field for CROSSTAB.

    Post your query attempt for analysis.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    CubsFan is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2020
    Posts
    4
    I do have the calculation in the value field, but what is happening is that it is performing the ratio calculation at the daily level and then summing the ratio across days to provide an incorrect ratio for the month. If I try to change the VALUE field to EXPRESSION, then i get an error that "your query does not include the specified expression 'iif....." as part of an aggregate

    TRANSFORM Sum(iif([CountofTrials]<=0,0, [CountofSuccessful_Trials]/[CountofTrials])) AS [State Ratio]
    SELECT tblDailyActivityImport.State
    FROM tblDailyActivityImport
    GROUP BY tblDailyActivityImport.State
    PIVOT Month([ActivityDate]);

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Provide sample data and desired result.

    If you want to provide db for analysis, follow instructions at bottom of my post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    CubsFan is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2020
    Posts
    4
    Database1.accdb

    Thank you for your help. Attached is a sample of the DB with the table and query. The query as it stands is providing a ratio of 3,117.99% for March and 2,520,.23% for April, which are incorrect. The expected ratios for Florida for March and April are 49.58% and 53.25% respectively. Thanks again for any guidance toward how to make this work.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Consider:

    TRANSFORM Sum(IIf([tblDailyActivityImport]![CountofTrials]<=0,0,[tblDailyActivityImport]![CountofSuccessful_Trials]))/Sum([tblDailyActivityImport]![CountofTrials]) AS [State Ratio]
    SELECT tblDailyActivityImport.State
    FROM tblDailyActivityImport
    GROUP BY tblDailyActivityImport.State
    PIVOT Month([ActivityDate]);

    If this is multi-year data, will want to have year as another Row Header.

    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    CubsFan is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2020
    Posts
    4
    Thank you. That did the trick.

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

Similar Threads

  1. Replies: 1
    Last Post: 01-26-2017, 08:29 AM
  2. calculation in Crosstab Query
    By tngirl in forum Queries
    Replies: 5
    Last Post: 03-13-2014, 12:31 PM
  3. Calculation Help, Either Query or Crosstab
    By mweisler20 in forum Queries
    Replies: 1
    Last Post: 06-30-2012, 01:44 PM
  4. Replies: 4
    Last Post: 03-27-2011, 01:24 AM
  5. crosstab query calculation
    By rutica in forum Queries
    Replies: 2
    Last Post: 04-28-2009, 09:52 AM

Tags for this Thread

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