我有以下表格:
Table2
StudentNumbers ExamType
------------------------
1234 1
2343 2
3345 5
3454 1
5465 2
...
Table1
StudentNumbers ExamType ExamDate School Area Info
------------------------------------------------------------------
1234 1 0825 warren ny 0x504B03042D0
1234 1 0829 north nj 0x63D86E1FFFF
1233 2 0921 north nj 0xA001400646F
2343 1 0922 warren ny 0x01400646174
2343 1 0925 north ny 0x100100070se
...
我正在尝试编写查询以获得以下结果:
StudentNumbers ExamType ExamDate School Area Info
-----------------------------------------------------------------
1234 1 0829 north nj 0x63D86E1FFFF
2343 1 0925 north ny 0x100100070se
我写了以下查询:
Select t1.StudentNumbers, t1.ExamDate, t1.School, t1.Info, t1.ExamType
from Table1 as t1
Join(
Select ts.StudentNumbers, max(ts.ExamDate) as ExamDate
from Table2 as ts
Join Table1 as pl on
ts.StudentNumbers = pl.StudentNumbers where ts.ExamType = pl.ExamType
group by ts.StudentNumbers
) as t2 on t1.StudentNumbers = t2.StudentNumbers
and t1.ExamDate = t2.ExamDate
上面的查询有效,并给出了我想要的结果,即基于 Table2 的特定 ExamType 的 max ExamDate。但是在这种聚合方法中使用多个连接是最好的方式吗?或者有没有更清洁的选择。