Results 1 to 6 of 6
  1. #1
    RealmOfConfusion is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Apr 2015
    Posts
    4

    Top 3 items in every product group

    Say I have a table that contains a list of product groups (Alpha, Bravo, Charlie), individual products within each group (A01, A02 etc. B01, B02 etc.) and the price of each product.

    What I need is to output the 3 highest value products within every product group, so the output would look something like this:


    GROUP PRODUCT PRICE
    Alpha A03 2617
    Alpha A07 2309
    Alpha A02 1926
    Bravo B04 983
    Bravo B05 983
    Bravo B02 872
    Charlie C02 3615
    Charlie C07 1762
    Charlie C01 760

  2. #2
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,842
    try something like this

    Code:
    SELECT *
    FROM myTable
    WHERE Product in (SELECT TOP 3 Product FROM myTable AS T WHERE Group=myTable.Group ORDER BY Price Desc)
    note Group is a reserved word, so using it will have unintended consequences, recommend you change it to ProdGroup or similar

  3. #3
    RealmOfConfusion is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Apr 2015
    Posts
    4
    Hi Ajax, thanks for the suggestion. Data table is called myTable, and I've changed by field names (prod_group, prod_name, prod_price), but using the following SQL amended from your suggestion, I'm getting a syntax error:

    SELECT *
    FROM myTable
    WHERE prod_name IN (SELECT TOP 3 prod_name FROM myTable AS T WHERE Group=myTable.Group ORDER BY prod_price Desc)

    It's probably me doing something incredibly obviously stupid, but any suggestions as to how to fix would be appreciated!

  4. #4
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,842
    Probably as previously advised

    note Group is a reserved word, so using it will have unintended consequences, recommend you change it to ProdGroup or similar

  5. #5
    RealmOfConfusion is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Apr 2015
    Posts
    4
    Yup, as expected, it was me being incredibly stupid. I'd assumed that your use of "group" was the group function and not my badly named field name.

    Changed both instances of group in your syntax to prod_group and that gives me exactly what I needed.

    Many thanks for your assistance.

  6. #6
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,842
    glad to help!

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

Similar Threads

  1. Replies: 6
    Last Post: 02-28-2015, 11:57 AM
  2. Top N items per group
    By babak in forum Queries
    Replies: 1
    Last Post: 08-02-2013, 10:38 AM
  3. Trying to group items on a Crosstab Report
    By Nick Lingenfelter in forum Reports
    Replies: 2
    Last Post: 02-27-2013, 07:08 AM
  4. Replies: 1
    Last Post: 10-22-2012, 08:04 AM
  5. Help in generating product group and Combobox
    By lm_lopes in forum Access
    Replies: 0
    Last Post: 02-28-2010, 05:44 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