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

方法一:

-- 方法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

猜你喜欢

转载自www.cnblogs.com/pluto-yang/p/12518240.html
今日推荐