Results 1 to 7 of 7
  1. #1
    videoclocknet is offline Novice
    Windows XP Access 2003
    Join Date
    May 2011
    Posts
    10

    SQL Query - Most recent date in a group - Select another field from the group

    Hi guys!


    I'm trying to make a so simple SQL Query and I'm embarrassed I Can't get it working. I've been looking around the web and found similar answers, but not exactly what I'm looking for.


    We've got 3 different tables:
    - CUSTOMERS table:
    Fields: CustomerID (PK), CustomerName,
    - PRODUCTS table:
    Fields: ProductID (PK), ProductName
    - PRIZES table:
    Fields: CustomerID (FK), ProductID (FK), Date, Prize


    Just to say that in Prizes table, a customer can have more than one prize for the same product at different dates. So for a given customer and a given product, only the most recent prize is the correct one.


    That said, I want to make a query that shows: for each customer its name and all the related products and its code->for each product only the most recent prize and the date that prize was stated


    Code:
    SELECT C.CustomerName, P.ProductID, P.ProductName, MAX(Pr.date)
    FROM Customers C, Products P, Prizes Pr
    WHERE C.CustomerID=Pr.CustomerID AND P.ProductID=Pr.ProductID
    GROUP BY C.CustomerName, P.ProductID, P.ProductName;

    But with such a query I can't show the prize.


    I've also tried with:
    Code:
    SELECT C.CustomerName, P.ProductID, P.ProductName, Pr.date, Pr.Prize
    FROM Customers C, Products P, Prizes Pr
    WHERE C.CustomerID=Pr.CustomerID AND P.ProductID=Pr.ProductID AND Pr.date =
        ( SELECT MAX(PRI.date)
          FROM pRIZES PRI
          WHERE PRI.CustomerID=Pr.CustomerID AND PRI.ProductID=PR.ProductID
        );

    But it shows me an error as well.
    Could you give a hand?



    Kind Regards

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,892
    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.

  4. #4
    videoclocknet is offline Novice
    Windows XP Access 2003
    Join Date
    May 2011
    Posts
    10
    Thanks guys for your support. Following the link you have provided to me, it seems that the query I'm looking for is as following:

    Code:
    SELECT C.CustomerName, P.ProductID, P.ProductName, Pr.date, Pr.Prize
    FROM Customers C, Products P, Prizes Pr
    WHERE C.CustomerID=Pr.CustomerID AND P.ProductID=Pr.ProductID AND Pr.date IN
       (SELECT TOP 1 PRI.date                            
       FROM Prizes AS PRI                             
       WHERE PRI.ProductID=PR.productID AND PRI.CustomerID=PR.CustomerID       
       ORDER BY PRI.date DESC) 
    ORDER BY C.CustomerName, P.ProductID, P.ProductName, Pr.date, Pr.Prize;
    Is it right?

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,892
    Did you test it?
    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.

  6. #6
    videoclocknet is offline Novice
    Windows XP Access 2003
    Join Date
    May 2011
    Posts
    10
    I will test it tomorrow when I arrive to my workplace and i'll let you know. I haven't got access to the DB from outside it.

    Thanks

  7. #7
    videoclocknet is offline Novice
    Windows XP Access 2003
    Join Date
    May 2011
    Posts
    10

    [SOLVED] SQL Query - Most recent date in a group - Select another field from the group

    Ey guys, the following query worked perfectly.
    Code:
    SELECT C.CustomerName, P.ProductID, P.ProductName, Pr.date, Pr.Prize
    FROM Customers C, Products P, Prizes Pr
    WHERE C.CustomerID=Pr.CustomerID AND P.ProductID=Pr.ProductID AND Pr.date IN
       (SELECT TOP 1 PRI.date                            
       FROM Prizes AS PRI                             
       WHERE PRI.ProductID=PR.productID AND PRI.CustomerID=PR.CustomerID       
       ORDER BY PRI.date DESC)
    Thanks for your support.

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

Similar Threads

  1. Replies: 3
    Last Post: 04-28-2015, 02:16 AM
  2. Replies: 17
    Last Post: 04-24-2014, 10:58 AM
  3. GROUP by yet SELECT fields not within GROUP BY
    By johnseito in forum Access
    Replies: 25
    Last Post: 11-03-2013, 10:20 PM
  4. Group by year where date in a query
    By Ruegen in forum Queries
    Replies: 1
    Last Post: 09-16-2013, 11:09 PM
  5. Replies: 5
    Last Post: 07-29-2011, 11:54 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