拉出最近的记录,包括加入 2 个表和过滤器

Pull up the most recent record including joining 2 tables and filters

拉上最近记录的帖子我看了很多。我一直没能找到一个包括加入另一个 table 和过滤器的。

我需要的是有关最近创建的文档(记录)的信息,但前提是它满足特定条件。另外,我需要从另一个 table.

中提取一些数据

s504 计划 Table

    Student ID   |   Firstname   |   Startdate   |   Status
    ----------       ---------       ---------       ------
    111111            Johnny         1/5/2015          F
    222222            Sue            4/7/2016          I
    333333            Barb           2/5/2016          F
    111111            Johnny         2/1/2016          F

案例Table

    Student ID   |   School   |   
    ----------       ------
    111111           Franklin
    222222           Eisenhower
    333333           Franklin

而我希望看到的结果只是文档状态为 F 的最新文档...

    Student ID  |  Firstname  |  Startdate  |  Status  |   School
    ----------     ---------     ---------     ------      ------
    111111          Johnny       2/1/2016        F         Franklin
    333333          Barb         2/5/2016        F         Franklin

谢谢!

您可以使用内部联接和 where

select 
    a.Student_ID
  , a.Firstname
  , a.Startdate
  , a.Status
  , b.School
from s504Plans  as a
inner join Cases  as b on  a.Student_ID = b.Student_ID
inner join ( select Student_ID, max(Startdate ) as max_startdate
            from s504Plans
            group by Student_ID) t 
           on ( a.Student_id = t.Student_id and a.Startdate = t.max_startdate)
where a.Status = 'F'