SqlServer-按日期取最大/最小的一条记录!
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
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)
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)
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')
还有另外一种写法,通过子查询形式
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
)
补充:两个日期之间的最大日期记录
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<A.createTime)
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<A.createTime)
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<A.createTime and CONVERT(varchar, createTime, 23) ='2019-04-10')
还有另外一种写法,通过子查询形式
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<A.createTime
)
补充:两个日期之间的最小日期记录
快来试试吧。
学习资料见知识星球。
以上就是今天要分享的技巧,你学会了吗?若有什么问题,欢迎在下方留言。
快来试试吧,小琥 my21ke007。获取 1000个免费 Excel模板福利!
更多技巧, www.excelbook.cn
欢迎 加入 零售创新 知识星球,知识星球主要以数据分析、报告分享、数据工具讨论为主;
1、价值上万元的专业的PPT报告模板。
2、专业案例分析和解读笔记。
3、实用的Excel、Word、PPT技巧。
4、VIP讨论群,共享资源。
5、优惠的会员商品。
6、一次付费只需99元,即可下载本站文章涉及的文件和软件。
共有 0 条评论