SqlServer-按日期取最大/最小的一条记录

创建表与表数据

CREATE TABLE tbMaxDate
(
    Guid uniqueidentifier Primary Key  default upper((replace(newid(),'-',''))) not null,
    Name varchar(255) not null,
    createTime datetime not null
)

insert into tbMaxDate(Guid,Name,createTime)values(NEWID(),'小绿','2019-04-10 09:38:53.793')
insert into tbMaxDate(Guid,Name,createTime)values(NEWID(),'小绿','2019-04-10 10:40:57.243')
insert into tbMaxDate(Guid,Name,createTime)values(NEWID(),'小绿','2019-04-11 09:56:53.433')
insert into tbMaxDate(Guid,Name,createTime)values(NEWID(),'小绿','2019-04-12 08:02:53.483')
insert into tbMaxDate(Guid,Name,createTime)values(NEWID(),'小红','2019-04-10 09:45:53.563')
insert into tbMaxDate(Guid,Name,createTime)values(NEWID(),'小红','2019-04-10 07:16:57.113')
insert into tbMaxDate(Guid,Name,createTime)values(NEWID(),'小红','2019-04-11 11:49:15.233')
insert into tbMaxDate(Guid,Name,createTime)values(NEWID(),'小红','2019-04-12 15:12:19.453')

select * from tbMaxDate order by name

SqlServer-按日期取最大/最小的一条记录_第1张图片

1.取全表最大日期的记录

select * from tbMaxDate A  where createTime=(select max(createTime) from tbMaxDate B where A.[Name]=B.[Name])

select * from tbMaxDate A where  not exists (select 1 from tbMaxDate where name=A.name and createTime>A.createTime) 

SqlServer-按日期取最大/最小的一条记录_第2张图片

2.取某个时间段最大日期记录

在规定的时间里取最大日期记录应该在max中多加上规定时间条件,如下两图对比

select * from tbMaxDate A  
where CONVERT(varchar, createTime, 23) ='2019-04-10'  
and createTime=(select max(createTime) from tbMaxDate B where A.[Name]=B.[Name])

select * from tbMaxDate A 
where CONVERT(varchar, createTime, 23) ='2019-04-10'    
and not exists (select 1 from tbMaxDate where name=A.name and createTime>A.createTime) 

SqlServer-按日期取最大/最小的一条记录_第3张图片

select * from tbMaxDate A  
where CONVERT(varchar, createTime, 23) ='2019-04-10'  
and createTime=(select max(createTime) from tbMaxDate B where A.[Name]=B.[Name] and CONVERT(varchar, createTime, 23) ='2019-04-10')

select * from tbMaxDate A 
where CONVERT(varchar, createTime, 23) ='2019-04-10'    
and not exists (select 1 from tbMaxDate where name=A.name and createTime>A.createTime and CONVERT(varchar, createTime, 23) ='2019-04-10') 

SqlServer-按日期取最大/最小的一条记录_第4张图片

还有另外一种写法,通过子查询形式


select * from tbMaxDate A  
where CONVERT(varchar, createTime, 23) ='2019-04-10'  
and createTime=(
    select max(createTime) from
    (
        select * from tbMaxDate 
        where CONVERT(varchar, createTime, 23) ='2019-04-10'
    ) B where A.[Name]=B.[Name]
)

select * from tbMaxDate A 
where CONVERT(varchar, createTime, 23) ='2019-04-10'    
and not exists (
    select 1 from 
    (
        select * from tbMaxDate  
        where CONVERT(varchar, createTime, 23) ='2019-04-10'
    ) temp where temp.name=A.name and temp.createTime>A.createTime
)

SqlServer-按日期取最大/最小的一条记录_第5张图片

补充:两个日期之间的最大日期记录

SqlServer-按日期取最大/最小的一条记录_第6张图片

3.取全表最小日期的记录

select * from tbMaxDate A  where createTime=(select MIN(createTime) from tbMaxDate B where A.[Name]=B.[Name])

select * from tbMaxDate A where  not exists (select 1 from tbMaxDate where name=A.name and createTime

SqlServer-按日期取最大/最小的一条记录_第7张图片

4.取某个时间段最小日期记录

在规定的时间里取最小日期记录可以在min中加上规定时间条件,也可以不加,如下两图对比

select * from tbMaxDate A  
where CONVERT(varchar, createTime, 23) ='2019-04-10'   
and createTime=(select MIN(createTime) from tbMaxDate B where A.[Name]=B.[Name])

select * from tbMaxDate A 
where CONVERT(varchar, createTime, 23) ='2019-04-10'  
and not exists (select 1 from tbMaxDate where name=A.name and createTime

SqlServer-按日期取最大/最小的一条记录_第8张图片

select * from tbMaxDate A  
where CONVERT(varchar, createTime, 23) ='2019-04-10'   
and createTime=(select MIN(createTime) from tbMaxDate B where A.[Name]=B.[Name] and CONVERT(varchar, createTime, 23) ='2019-04-10')

select * from tbMaxDate A 
where CONVERT(varchar, createTime, 23) ='2019-04-10'  
and not exists (select 1 from tbMaxDate where name=A.name and createTime

SqlServer-按日期取最大/最小的一条记录_第9张图片

还有另外一种写法,通过子查询形式

select * from tbMaxDate A  
where CONVERT(varchar, createTime, 23) ='2019-04-10'  
and createTime=(
    select MIN(createTime) from
    (
        select * from tbMaxDate 
        where CONVERT(varchar, createTime, 23) ='2019-04-10'
    ) B where A.[Name]=B.[Name]
)

select * from tbMaxDate A 
where CONVERT(varchar, createTime, 23) ='2019-04-10'    
and not exists (
    select 1 from 
    (
        select * from tbMaxDate  
        where CONVERT(varchar, createTime, 23) ='2019-04-10'
    ) temp where temp.name=A.name and temp.createTime )

SqlServer-按日期取最大/最小的一条记录_第10张图片

补充:两个日期之间的最小日期记录

SqlServer-按日期取最大/最小的一条记录_第11张图片

 

你可能感兴趣的:(SqlServer)