Results 1 to 13 of 13
  1. #1
    marcustofeles is offline Novice
    Windows XP Access 2002
    Join Date
    Oct 2011
    Posts
    23

    Limit query to only show the latest record

    Hi all, crossforum double-post here in desperation...



    I'm trying to put together a small database for tracking and tracing a fleet of shipping containers. One function I need is to query which containers that are currently at one specific location.
    The query below helps me to see what units are at a certain location, based on the input of the location name in a form field. However, if the same container have visited the location more than once then it shows up twice (or more).

    How can I limit the query to not show container duplicates? Also, I want the most recent dated location-visit to show, but I'm guessing that the ORDER BY-criteria take care of that.

    SELECT Tracking.Container, Tracking.Location, Tracking.Arrivaldate, Tracking.Loaded, Tracking.Empty
    FROM Tracking
    WHERE (((Tracking.Location)=[Forms]![Containerpositioning]![location]))
    ORDER BY Tracking.Arrivaldate DESC;

    Regards
    Marcus

  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,870
    Marcus,
    Can you show your tables and relationships --- Tools-->Relationships as a jpg?
    It would help us understand your db.
    It is unclear what field represents
    most recent dated location-visit
    .
    You will probably need a subquery along this line

    ..... Where ShipDate = ( Select Max(ShipDate) from Tracking .....)

  3. #3
    marcustofeles is offline Novice
    Windows XP Access 2002
    Join Date
    Oct 2011
    Posts
    23
    Hi Orange,

    Here you go.
    I dont't have any relationships at all...

    Attachment 4660


    Much obliged

  4. #4
    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,870
    If you only have 1 table, and the PK is the ID of the container, then you can only have 1 record per container.

    Please describe your situation with sample data.

  5. #5
    marcustofeles is offline Novice
    Windows XP Access 2002
    Join Date
    Oct 2011
    Posts
    23
    The primary key is not the container, it's a default ID given by the system (in laymen terms...)

    Can I upload the database here?

  6. #6
    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,870
    According to the graphic you sent, the ID is the PK, but I guess it is just a record id.
    I believe you can upload your mdb here.

  7. #7
    marcustofeles is offline Novice
    Windows XP Access 2002
    Join Date
    Oct 2011
    Posts
    23
    Here is the database. My apologies for it being in swedish, hopefully you'll be able to understand anyway!

    Here is also a screenshot of what field in the form that, filled in, is a condition for the query.



    All your help is greatly appreciated.
    Br Marcus
    Last edited by marcustofeles; 10-05-2011 at 06:10 AM. Reason: political reasons

  8. #8
    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,870
    Marcus,
    I'm not sure what I'm looking for????
    You have a filter on the data on your form

    [container]='NOBU 111111-1'

  9. #9
    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,870
    Marcus,
    Adjust your query to this

    SELECT Tracking.Container, Tracking.Position, Tracking.Ankomstdatum, Tracking.Lastad, Tracking.Tom
    FROM Tracking
    WHERE
    Tracking.Ankomstdatum = (Select max(Ankomstdatum) from Tracking
    where (((Tracking.Position)=[Forms]![Containerpositionering]![position])))
    )
    ;
    This will only show the record with the latest Ankomstdatum for this
    Position =[Forms]![Containerpositionering]![position]

  10. #10
    marcustofeles is offline Novice
    Windows XP Access 2002
    Join Date
    Oct 2011
    Posts
    23
    Hello Mr Orange!

    I'm afraid I can't find the filter that you mentioned...

    Your query did indeed return only the latest unit for a certain position, but it only showed one container, there should be more units at the position I queried.

    Perhaps it's not possible to solve this with the setup I have?


    I'm unbelievably grateful for your input.
    Cheers
    Marcus

  11. #11
    marcustofeles is offline Novice
    Windows XP Access 2002
    Join Date
    Oct 2011
    Posts
    23
    Hey man,

    Seems to be solved.

    SELECT Tracking.Container, Tracking.Position, Tracking.Lastad, Tracking.Tom, Tracking.Ankomstdatum
    FROM Tracking INNER JOIN POS ON (Tracking.Container = POS.Container) AND (Tracking.Ankomstdatum = POS.Ankomstdatum)
    WHERE (((Tracking.Position)=[Forms]![Containerpositionering]![position]));

    Again, thank you for your efforts!

  12. #12
    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,870
    Glad you got it under control.
    Does it really give you the most recent dated location-visit.

    I must admit I did/do not really understand that expression.
    My thought was that if you had multiple
    Ankomstdatum then you only wanted the latest.I don't see how your solution does that, but as I said, I don't really understand the terms.
    Glad you got it working.


  13. #13
    marcustofeles is offline Novice
    Windows XP Access 2002
    Join Date
    Oct 2011
    Posts
    23
    So far it works...

    Cheers mate

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

Similar Threads

  1. Get latest record
    By Sweet16 in forum Queries
    Replies: 9
    Last Post: 09-23-2011, 01:39 PM
  2. Show only last record in query
    By robsworld78 in forum Queries
    Replies: 18
    Last Post: 09-02-2011, 08:36 PM
  3. Limit query to one record
    By sotssax in forum Queries
    Replies: 3
    Last Post: 07-28-2011, 01:50 PM
  4. Code to display latest record number field created?
    By rowardHoark in forum Programming
    Replies: 1
    Last Post: 01-31-2011, 08:03 AM
  5. Query latest record
    By asherbear in forum Queries
    Replies: 5
    Last Post: 08-02-2010, 03:58 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