Sqlite查询用于获取具有不同patientID的最新考试日期时间

时间:2015-09-24 13:05:56

标签: sql database sqlite

在Sqlite db中我有一个表:使用列ExamID,InternalPID,ExamDateTime

列进行检查
ExamID InternalPID ExamDateTime (from left to right) 
1 2 2015-03-11
2 1 2015-11-11 
3 4 2015-05-01 
4 6 2015-08-10 
5 2 2015-04-22 
6 1 2014-12-11 
7 2 2015-03-12

查询输出应该是每个患者的最新检查日期。即InternalPID应与其最新的ExamDateTime不同。

期望来自查询的输出:

ExamID InternalPID ExamDateTime
5 2 2015-04-22
2 1 2015-11-11
3 4 2015-05-01
4 6 2015-08-10

提前谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用连接和聚合或聪明的where子句来执行此操作:

select e.*
from examination e
where e.ExamDateTime = (select max(e2.ExamDateTime)
                        from examination e2
                        where e2.patientid = e.patientid
                       );