Results 1 to 8 of 8
  1. #1
    davecumber is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jul 2012
    Posts
    8

    Select most recent related record from related Table

    I have a DB with customers in one table and then related tables that contain each time we speak to them, each time they order something and each time there is a meeting with them.

    I've been asked to setup a query that will show the user's details, from the customers table and then the most recent dated entry in the other 3 tables.
    I know that this is asked for a lot and have tried a few of the solutions I have found but I get a variety of errors from each when I try them. I'm not sure if I've been translated the table names incorrectly.

    Can anyone help me? I have got so far which gives me a list of all companies, with a date set in the column for those that we have never contacted.

    Code:



    Select contacts.*,iif(isnull([date chased]),'31/12/9999',[date chased]) AS [Date_Chased] from contacts LEFT JOIN [chases table] on contacts.id = [chases table].company
    Thanks

    Dave

  2. #2
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    You could try using the aggregate select query on the remaining 3 tables, by using the GROUP BY on the company & MAX on the Date field.
    Then try joining them on the Contacts table with a LEFT JOIN with the Contacts Table on the left.

    Thanks

  3. #3
    davecumber is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jul 2012
    Posts
    8
    Is there any chance I could ask you to write out a quick example of what you mean, maybe with just one of the joined tables. Every time I think I am getting somewhere with the logic, I get an error. It seems like as soon as I start changing my queries I get errors.

  4. #4
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    Just check if below gives some guidelines :
    Code:
    SELECT 
        tblContacts.contactID, 
        tblContacts.contactName, 
        tblContacts.contactEmail, 
        qrySpokenContactsMaxDate.ContactID_FK, 
        qrySpokenContactsMaxDate.MaxOfSpokenDate, 
        qryMeetingsContactsMaxDate.ContactID_FK, 
        qryMeetingsContactsMaxDate.MaxOfMeetingDate, 
        qryContactsOrdersMaxDate.ContactID_FK, 
        qryContactsOrdersMaxDate.MaxOfOrderDate
    FROM 
        (
            (
                tblContacts 
                LEFT JOIN 
                (
                    SELECT 
                        tblSpoken.ContactID_FK, 
                        Max(tblSpoken.SpokenDate) AS MaxOfSpokenDate
                    FROM 
                        tblSpoken
                    GROUP BY 
                        tblSpoken.ContactID_FK
                )
                AS qrySpokenContactsMaxDate 
                ON 
                tblContacts.contactID = qrySpokenContactsMaxDate.ContactID_FK
            ) 
            LEFT JOIN 
            (
                SELECT 
                    tblMeetings.ContactID_FK, 
                    Max(tblMeetings.MeetingDate) AS MaxOfMeetingDate
                FROM 
                    tblMeetings
                GROUP BY 
                    tblMeetings.ContactID_FK
            ) 
            AS qryMeetingsContactsMaxDate 
            ON 
            tblContacts.contactID = qryMeetingsContactsMaxDate.ContactID_FK
        ) 
        LEFT JOIN 
        (
            SELECT 
                tblOrders.ContactID_FK, 
                Max(tblOrders.OrderDate) AS MaxOfOrderDate
            FROM 
                tblOrders
            GROUP BY 
                tblOrders.ContactID_FK
        )
        AS qryContactsOrdersMaxDate 
        ON 
        tblContacts.contactID = qryContactsOrdersMaxDate.ContactID_FK;
    Thanks

  5. #5
    davecumber is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jul 2012
    Posts
    8
    Thanks, That has worked perfectly.

  6. #6
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    Glad you found it helpful.

    Thanks

  7. #7
    davecumber is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jul 2012
    Posts
    8
    Yes, really helpful, thank you again. Of all the solutions I've seen, this was the first I could put into use. Plus I was able to understand what it is doing by simply reading the query.

  8. #8
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    Quote Originally Posted by davecumber View Post
    I was able to understand what it is doing by simply reading the query.
    I learnt this from r937 at http://forums.devshed.com/ms-sql-development-95/.
    Great guy.

    Thanks

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

Similar Threads

  1. Go To Record with related table Primary Key
    By jamiebull21 in forum Forms
    Replies: 3
    Last Post: 02-17-2012, 09:36 AM
  2. ...related record required...
    By degras in forum Forms
    Replies: 11
    Last Post: 11-03-2011, 12:20 PM
  3. Replies: 12
    Last Post: 09-09-2011, 11:14 AM
  4. Add Record based on related value
    By top1hat19 in forum Access
    Replies: 0
    Last Post: 03-08-2011, 12:45 PM
  5. Foce new record in related table?
    By thekruser in forum Access
    Replies: 1
    Last Post: 11-08-2010, 02:00 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