34,587
社区成员
发帖
与我相关
我的任务
分享
select * from tb a where not exists(
select 1 from tb where From_name=a.From_name and send_time>a.send_time
)
create table tb
(From_name nvarchar(10),send_time datetime)
insert into tb
select '张三' ,'2011-09-11' union all
select '张三' ,'2011-09-12' union all
select '张三' ,'2011-09-13' union all
select '李四' ,'2011-09-10' union all
select '李四' ,'2011-09-14' union all
select '王五' ,'2011-09-01'
---获取最小时间
;with cte as (
select *,rowNum=ROW_NUMBER() over(partition by From_name order by send_time) from tb
)
select * from cte where rowNum=1
---获取最大时间
;with cte as (
select *,rowNum=ROW_NUMBER() over(partition by From_name order by send_time desc) from tb
)
select * from cte where rowNum=1