Results 1 to 8 of 8
  1. #1
    anthgav is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    86

    Field which calculates a percentage between 2 numbers from other fields

    Hi



    I'm trying to calculate a percentage between to other fields in an Access query.

    Here is the query
    Click image for larger version. 

Name:	Query.PNG 
Views:	11 
Size:	19.5 KB 
ID:	30329

    SQL view
    SELECT tblBinType.bin_type, Count(tblAllocatedBin.allocated_bin_type) AS CountOfallocated_bin_type, Count(tblProduct.allocated_bin) AS CountOfallocated_bin, Sum([tblProduct.allocated_bin]/[tblAllocatedBin.allocated_bin]*100) AS [Capacity %]
    FROM tblBinType RIGHT JOIN (tblAllocatedBin LEFT JOIN tblProduct ON tblAllocatedBin.allocated_bin_id = tblProduct.allocated_bin) ON tblBinType.bin_type_id = tblAllocatedBin.allocated_bin_type
    GROUP BY tblBinType.bin_type;




    Here is the result
    Click image for larger version. 

Name:	Query 2.PNG 
Views:	11 
Size:	9.2 KB 
ID:	30330




    As you can see in the result the "Capacity %" field does not calculate the percentage between fields 2 & 3 correctly. Can have some assistance in working this out please?

    Thanks

  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
    Try this untested shot-in-the-dark:

    SELECT tblBinType.bin_type, Count(tblAllocatedBin.allocated_bin_type) AS CountOfallocated_bin_type, Count(tblProduct.allocated_bin) AS CountOfallocated_bin, Count([tblProduct.allocated_bin])/Count[tblAllocatedBin.allocated_bin])*100 AS [Capacity %]
    FROM tblBinType RIGHT JOIN (tblAllocatedBin LEFT JOIN tblProduct ON tblAllocatedBin.allocated_bin_id = tblProduct.allocated_bin) ON tblBinType.bin_type_id = tblAllocatedBin.allocated_bin_type
    GROUP BY tblBinType.bin_type
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    anthgav is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    86
    Works perfectly, thank you Sir.

    Just one missing opening bracket in your query...
    Count([tblProduct.allocated_bin])/Count[tblAllocatedBin.allocated_bin])*100

    Should be
    Count([tblProduct.allocated_bin])/Count([tblAllocatedBin.allocated_bin])*100

    The result...
    Click image for larger version. 

Name:	Query.PNG 
Views:	10 
Size:	8.6 KB 
ID:	30331



    Just one more thing, how to I round the result to a whole number please?

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    Use Round() function or DecimalPlaces property of textbox.
    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
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I'll get out of the way.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    anthgav is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    86
    I tried the Round() function like this highlighted in red but I'm getting a "Syntax error (missing operator) in query expression" message.

    Code:
    SELECT tblBinType.bin_type, Count(tblAllocatedBin.allocated_bin_type) AS CountOfallocated_bin_type, Count(tblProduct.allocated_bin) AS CountOfallocated_bin, Round(Count([tblProduct.allocated_bin])/Count([tblAllocatedBin.allocated_bin])*100 AS [Capacity %], 1)
    FROM tblBinType RIGHT JOIN (tblAllocatedBin LEFT JOIN tblProduct ON tblAllocatedBin.allocated_bin_id = tblProduct.allocated_bin) ON tblBinType.bin_type_id = tblAllocatedBin.allocated_bin_type
    GROUP BY tblBinType.bin_type

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    AS [Capacity %] goes outside the function.
    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.

  8. #8
    anthgav is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    86
    Thanks guys, much appreciated.

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

Similar Threads

  1. Percentage Difference Between two fields?
    By natonstan in forum Queries
    Replies: 17
    Last Post: 04-15-2017, 10:18 PM
  2. Replies: 1
    Last Post: 10-09-2012, 09:08 AM
  3. Replies: 5
    Last Post: 04-24-2012, 01:56 PM
  4. Replies: 6
    Last Post: 10-21-2011, 10:24 PM
  5. Add fields and find percentage
    By gurp99 in forum Queries
    Replies: 1
    Last Post: 08-09-2010, 03:57 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