Results 1 to 7 of 7
  1. #1
    Foyer is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2021
    Posts
    12

    Syntax for SELECT TOP (I think) Query

    Hi all,

    I'm trying to build a report that shows every product category and then lists only a few products (not all products) within each category. I want to limit the products to no more than 3-5 for each category. I keep thinking I need to use a SELECT TOP query but the more I research it the more it seems it should only work with calculated fields so maybe I'm going about it the wrong way? I can't seem to figure out the syntax for the query to build the report.



    The two fields I am using are ProductName and ProductCategory from the ProductsT table, so the report would be grouped by ProductCategory with a selection of 3-5 ProductName below it.

    Thanks for your advice.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Why would you think it only works with calculated fields?

    Review http://allenbrowne.com/subquery-01.html#TopN
    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
    Foyer is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2021
    Posts
    12
    Just all of the tutorials I could find always included a calculated field so I thought maybe that's what I'm missing.

    I built two test tables with the fields from a couple of the examples in your link and some sample data and then copied the SQL in the hopes I could better understand how they worked but neither query returned the proper results.

    One was the meter table for the "Get the value in another record" example and the other was the Orders Table for the "Top n Records per group" example. I'm sorry, I'm not a database designer and this coding stuff is super confusing to me.

    Tanya

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    It's a proven technique and a frequently referenced tutorial.

    Would have to see what you did to advise further. Or your actual data. Can attach file to post. 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
    Foyer is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2021
    Posts
    12
    Here it is: Widgets.zip

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Does this give what you expect:

    SELECT * FROM ProductT WHERE ProductID IN (SELECT TOP 3 ProductID FROM ProductT AS Dupe WHERE Dupe.Category=ProductT.Category ORDER BY ProductID);
    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
    Foyer is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2021
    Posts
    12
    Yes! Thank you!

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

Similar Threads

  1. Replies: 21
    Last Post: 01-03-2019, 08:06 PM
  2. Convert SQL Server Syntax To Access Query Syntax
    By chalupabatman in forum Queries
    Replies: 1
    Last Post: 10-18-2017, 08:53 PM
  3. Replies: 3
    Last Post: 05-13-2015, 08:36 AM
  4. Syntax error on VB Select Where Clause
    By FrustratedAlso in forum Programming
    Replies: 3
    Last Post: 01-30-2014, 02:57 AM
  5. Select statement syntax?
    By ksmith in forum Programming
    Replies: 3
    Last Post: 06-24-2010, 09:21 AM

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