Results 1 to 10 of 10
  1. #1
    codingman is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2021
    Posts
    5

    Smile MS Access help this may be simple to do but bear with me


    If I had a table in a database with items and their prices and I wanted to do a query to get not only the highest price using the MAX function but also the item with that highest price what would I do?

    I tried this already but for some reason could not get it to work, is it possible for someone to give an example please? Or say how it would be done. I know you use the max function for the highest price but when it came to getting the item or itemname itself it was just a problem.

    Thank you.

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    Would be easier to assist if you provided the SQL of the query you have tried.

  3. #3
    codingman is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2021
    Posts
    5
    Thank you so much for replying and so quickly too.

    This is the SQL
    but the thing is in this I just got the highest price but I also wanted the name of that item with the highest price from the Inventory Table


    SELECT DISTINCTROW Max(Inventory.Price) AS MaxOfPrice
    FROM INVENTORY;

    Not sure if this helps but thank you.

  4. #4
    JoeM is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    3,904
    Assuming the name of the Item field is "Item", try something like this:
    Code:
    SELECT Item, Price
    FROM Inventory
    WHERE Price = 
    (SELECT Max(Inventory.Price) AS MaxOfPrice
    FROM Inventory);

  5. #5
    codingman is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2021
    Posts
    5
    Again thank you I was trying to test it but for some reason my MS Access isn't working.

    But I'm really glad you came up with this solution.

  6. #6
    JoeM is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    3,904
    What is the name of your Item field?
    Did you coipy/paste this code directly into the "SQL View" of a Query?

  7. #7
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    since this is a subquery you need to alias one of the references to inventory

    Code:
    SELECT Item, Price
    FROM Inventory
    WHERE Price = 
    (SELECT Max(Price) AS MaxOfPrice
    FROM Inventory as T)
    Alternatively treat as a virtual table which will be more efficient for larger datasets

    Code:
    SELECT A.Item, A.Price
    FROM Inventory A
        INNER JOIN 
            (SELECT Max(Price) AS MaxOfPrice FROM Inventory) B
               ON A.Price=B.MaxOfPrice

  8. #8
    codingman is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2021
    Posts
    5
    I wanted to but I can't get my ms access working at the moment so I have to wait before I can test it.

  9. #9
    codingman is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2021
    Posts
    5
    Thank you For your reply I will try this solution as well.

  10. #10
    JoeM is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    3,904
    I wanted to but I can't get my ms access working at the moment so I have to wait before I can test it.
    OK. I misunderstood.
    I thought you meant that my solution wasn't working for you, but see you mean that you are actually having a problem getting MS Access to work.

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

Similar Threads

  1. Replies: 2
    Last Post: 11-10-2017, 07:52 PM
  2. Replies: 8
    Last Post: 03-16-2016, 10:11 AM
  3. Replies: 16
    Last Post: 05-04-2014, 09:19 AM
  4. excel link with access differs from simple export from access
    By M0RDANT in forum Import/Export Data
    Replies: 4
    Last Post: 03-25-2013, 02:43 PM
  5. Replies: 0
    Last Post: 10-21-2010, 08:24 AM

Tags for this Thread

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