导航

SQL查询时如何去掉重复记录后保留最新的记录

Posted on 2018-12-21 16:29  咸鱼辣椒  阅读(617)  评论(0)    收藏  举报


select * from problem_shipment ps where ps.case_id is not null and not exists (select 1 from problem_shipment where case_id = ps.case_id and reported_date > ps.reported_date);

 

if object_id('tempdb.dbo.#') is not null drop table #

create table #(姓名 varchar(8), 日期 datetime)
insert into #
select '张三', '2009-01-01' union all
select '张三', '2010-10-10' union all
select '李四', '2010-1-10'
select * from # t where not exists (select 1 from # where 姓名=t.姓名 and 日期>t.日期)