Results 1 to 3 of 3
  1. #1
    katjoe is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2024
    Posts
    14

    Query with sub query for aggregating values

    Hi I am not able to share the code due to the database in on a secured network. Perhaps I can modify it so I will get back to you with this.
    On that note could you help with a query though?
    What I am trying to do is:

    1. Get the distinct values of the field 1.
    2. Find the percentage of the Distinct Values by dividing the distinct value by the total value of field 1.
    3. Counting all values in the three fields of the table then divide it by the distinct values to get the overall percentage.

    Desired results:
    Field 1 Distinct_Field Percentage_Field Total_Percentage_Field
    X 3 1% .01%
    Y 1 .01% .001%
    Z 2 .02% .02%

    Modified version of Query one that will count the values in three columns.

    <CODE>
    SELECT Count([Field Table].[Field 1]) as Total_Field, Count([Field Table].[Location]) AS Total_Location, Count([Field Table].[Cars]) AS Total_Cars FROM Dealer;
    </code>
    Second query that will pull the results from query 1:
    <code>


    SELECT [Field Table].[Field 1], Count([Field Table].[Field 1]) As Distinct_Field, ([Distinct_Field] / Total_Field]) As Percentage_Field, ([Distinct_Field] / ( [Total_Field], [Total_Location], [Total_Cars])) AS Total_Percentage_Field
    From Query
    Where [Field Table].[Field 1] Is Not Null
    GROUP BY [[Field Table].[Field 1];

    Access does not like this. Not sure how to combined the distinct and apply the aggregate functions to get the above results.
    Any Help is appreciated!!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    CODE tags use [] not <>.

    [Field 1] is not in query 1. Query 1 aggregates and assigns alias name to expressions. Why not put filter criteria in query 1?

    Provide sample raw data. What has db being on a secured network got to do with sharing code?

    Strongly advise not to use spaces nor special characters in naming convention.
    Last edited by June7; 10-30-2024 at 09:52 PM.
    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
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,931
    why are you starting a new thread when you have already asked the question here?
    https://www.accessforums.net/showthread.php?t=90342

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

Similar Threads

  1. Replies: 6
    Last Post: 08-31-2015, 09:50 AM
  2. Replies: 1
    Last Post: 10-14-2014, 11:26 AM
  3. Aggregating fields in Select Query
    By becka11 in forum Queries
    Replies: 3
    Last Post: 05-03-2013, 09:55 AM
  4. Aggregating Data
    By mfarley in forum Queries
    Replies: 5
    Last Post: 02-01-2013, 09:13 AM
  5. Replies: 2
    Last Post: 04-29-2012, 02:22 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