Return Record Matching Latest Date

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BlackEyedPea
    New Member
    • Dec 2009
    • 9

    Return Record Matching Latest Date

    Hi there,
    I have a table, which has an order number, a comment & a date the comment was added.

    Therefore you could have 3 or 4 comments against one order number. All I want to do is create a table via a query which takes my existing table and shows me the latest comment based on the date. So for example, if there are two comments for an order, one done today and one yesterday I want my query to show me only the comment done today.

    I thought this would be easy, but when I use the MAX on the date on the query designer I am still getting two rows of data for some orders.

    I just can't seem to get my head around what I am doing wrong.
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    Please post the SQL for the query concerned - we can't answer you without having the details of the tables and fields concerned in front of us.

    -Stewart

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32645

      #3
      First of all let me warn you against duplicating the pre-existing data into a separate table. This is very rarely a good idea (See Database Normalisation and Table Structures). Instead, just use the SELECT query when that specific data is required.

      Next, please check the new title of the thread. This is not only a valid title, but it also gives you clues as to what you're actually trying to do. Please remember in future to use a sensible title for any new question threads.

      Now let's look at your actual issue. You're not looking to return the value of the maximum date, but actually you want the record associated with that maximum. This can be done in the following format :
      Code:
      SELECT *
      FROM   [YourTable] AS [YTo]
      WHERE  [CommentDate] In(SELECT Max([CommentDate])
                              FROM   [YourTable] AS [YTi]
                              WHERE  YTi.OrderNo = YTo.OrderNo)
      Does that make sense?

      Comment

      • BlackEyedPea
        New Member
        • Dec 2009
        • 9

        #4
        Thank you very much for your help, I've tried what you said above and it worked straight away.

        Thanks again.

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32645

          #5
          I'm very pleased to help, and almost more pleased even to hear that what I knocked up worked first time. Very gratifying :-)

          Comment

          Working...