简体   繁体   中英

MS Access SQL - Most Recent Record for Each Consultant ID

I have spent some time on StackOverflow looking for this answer and trying a bunch of these solutions without luck. I feel like I am missing something minor but I cannot resolve it. NOTE - Learning SQL and using Access because that is what work uses.

I have 2 tables, 1 has consultant information in it (Columns:Consultant ID, First Name, Last Name, Active (Yes or No Checkbox). The 2nd table has their Weekly Numbers ( Columns: AutoGenID, Consultant ID, WeekOf (Date), FullService, Consulting, Classified, Reallocations, RecruitmentSrvs) and joined them on ConsultantID (primary key)

I built a simply query to Join the 2 tables and show all the results ONLY for the active consultants in qry_Join (anyone marked not active does not show in this query) qry_Join returns Consultant ID, First Name, & Last Name (From tbl_Consultants) and then WeekOf (Date), FullService, Consulting, Classified, Reallocations, RecruitmentSrvs from tbl_WeeklyNumbers.

Question:

I would like to have a query that shows ONLY the most recent WeekOf (Date)entry by each consultant.

Issue:

SQL I am using is below but the issue I am having is that if ConsultantID #'s 3, 4, 5, 6, & 7 use a 10/11/2017 date and then ConsultantID #8 uses a 10/12/2017, the query will only return ConsultantID #8's row since it is most recent. I need it to still return all the other consultants most recent rows as well even if they are a date prior to ConsultantID #8s'

SQL:

SELECT ConsultantID, FirstName, WeekOf, USFullService, USConsulting, Classified, Reallocations, RecruitmentSrvs
FROM qry_Join
Where WeekOf = (SELECT MAX(WeekOf) FROM qry_Join)

Just pass ConsultantID to your subquery:

SELECT ConsultantID, FirstName, WeekOf, USFullService, USConsulting, Classified, Reallocations, RecruitmentSrvs
FROM qry_Join q
Where WeekOf = (SELECT MAX(s.WeekOf) FROM qry_Join s WHERE s.ConsultantID = q.ConsultantID)
SELECT  ConsultantID, 
        FirstName, 
        WeekOf, 
        USFullService, 
        USConsulting, 
        Classified, 
        Reallocations, 
        RecruitmentSrvs
   FROM qry_Join INNER JOIN
        (SELECT MAX(WeekOf) maxwkof ,ConsultantID cid
          FROM qry_Join 
          GROUP BY ConsultantID )
   ON ConsultantID = cid  
     WHERE maxwkof = WeekOf

The issue in your query is, The below query gives a max date in the whole table. And that max id Matches only one consultant. SO you get one row

 WeekOf = (SELECT MAX(WeekOf) FROM qry_Join)

The below query will give the max date of each consultant. SO you can join the max date and Consultant ID to get the details for each consultant in their latest weekoff

SELECT MAX(WeekOf) maxwkof ,ConsultantID cid
              FROM qry_Join 
              GROUP BY ConsultantID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM