Results 1 to 8 of 8
  1. #1
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305

    Suggestions to better optimize percentile query?

    I have a table of complaints. There's a category field and dollar amount field. I created a query to group by category and then calculate the 10th percentile dollar value for each category by using a subquery. Below is the SQL. It works but runs very slow when opening. There's only 1,000 total records so I'm thinking my SQL just isn't designed well.



    Any suggestions on how to better optimize it?

    Code:
    SELECT tblComplaints.ComplaintType, Max(tblComplaints.[DollarAmount]) AS 10thPercentileFROM tblComplaints
    WHERE (((tblComplaints.[DollarAmount]) In (SELECT TOP 10 PERCENT [DollarAmount]          FROM tblComplaints AS sub          WHERE sub.ComplaintType= tblComplaints.ComplaintType         ORDER BY [DollarAmount] ASC     )))
    GROUP BY tblComplaints.ComplaintType;
    Last edited by templeowls; 02-23-2024 at 01:09 PM.

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Have you tried saving the sub query as a separate saved query and joining on it?

    My understanding is that Access doesn't optimise sub-queries very well if at all.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305
    Very dumb question, but how would I do that? Not sure given the where statement in the subquery

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I agree. IIRC that sub query will run once for every record in the main query, and it is made worse when the sub query contains aggregate functions.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Unusually, I can't quite decipher the overall purpose of the query from reading its SQL (possibly because it is late afternoon on a Friday here!)

    I think you would need to use
    Code:
    SELECT TOP 10 PERCENT [DollarAmount], RiskGroup FROM tblComplaints ORDER BY [DollarAmount] ASC
    As your saved query but I'm not 100% certain.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  6. #6
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305
    Quote Originally Posted by Minty View Post
    Unusually, I can't quite decipher the overall purpose of the query from reading its SQL (possibly because it is late afternoon on a Friday here!)

    I think you would need to use
    Code:
    SELECT TOP 10 PERCENT [DollarAmount], RiskGroup FROM tblComplaints ORDER BY [DollarAmount] ASC
    As your saved query but I'm not 100% certain.
    Yea this was what I tried :/. It doesn't work because it's filtering to the top 10 percent across the entire table. Not top 10 percent specific to each ComplaintType (ignore 'riskgroup'; that was an error on my part in my OP).

    I almost need to create a subquery for each complainttype and then join them all, but there's 60-70 different complainttypes :/

  7. #7
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    You could try indexing dollar amount or include the pk in your sub query and join on that instead of the dollar amount.

  8. #8
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    What I meant was

    Code:
    SELECT ComplaintType, Max([DollarAmount]) AS 10thPercentile
    FROM tblComplaints
    WHERE PK In (SELECT TOP 10 PERCENT PK FROM tblComplaints AS sub          
                               WHERE sub.ComplaintType= tblComplaints.ComplaintType
                               ORDER BY [DollarAmount] ASC)
    
    GROUP BY ComplaintType;

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

Similar Threads

  1. Query to optimize warehouse locations
    By Sunshack in forum Queries
    Replies: 4
    Last Post: 02-25-2019, 12:03 PM
  2. how to do 75th percentile for a query field
    By vicki58 in forum Queries
    Replies: 3
    Last Post: 12-20-2013, 09:27 AM
  3. Calculating percentile with query
    By glmtd in forum Queries
    Replies: 5
    Last Post: 07-26-2012, 01:35 AM
  4. Replies: 1
    Last Post: 11-16-2011, 11:56 PM
  5. Optimize a select query
    By accessnewb in forum Queries
    Replies: 15
    Last Post: 07-21-2011, 01: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