按某一列分组并筛选时间最新的一条记录

方法一:

-- 方法1
select a.* 
 from table a
 where not exists(select 1 
                  from table b
                  where b.id_no=a.id_no and b.crt_time>a.crt_time)

 

方法二:子查询

-- 方法2
select a.*
 from table a
 inner join
 (select id_no,
         max(crt_time) max_time
  from table 
  group by id_no) b on a.id_no=b.id_no and a.crt_time=b.max_time

 

posted on 2020-03-18 16:35  平平无奇杨小兵  阅读(207)  评论(0编辑  收藏  举报