摘要:
下文讲述使用sql脚本,获取群组后记录的第一条数据
业务场景说明:
学校教务处要求统计:
每次作业,最早提交的学生名单
下文通过举例的方式,记录此次脚本编写方法,方便以后备查,如下所示:
实现思路:
使用开窗函数,对数据进行分组并按照提交时间进行排序后生成新的组内编号,如下所示:
`/
over开窗函数中 partition by分组
order by 排序
/
create table test(keyId int identity,
keChengName nvarchar(20),
name nvarchar(20),
inDate datetime)
go
insert into test(keChengName,name,inDate)
values('语文','猫猫','2018-9-1 10:00:00'),
('语文','maomao365','2018-9-1 13:00:00'),
('数学','sql教程','2018-9-1 8:00:00'),
('数学','sql博客教程','2018-9-1 9:00:00'),
('数学','其它','2018-9-1 8:10:00')
select from
(
select ,
row_number() over(partition by keChengName
order by inDate asc )
as newKeyId
from test
) as t where t.newKeyId =1
go
truncate table test
drop table test `
转载于:https://blog.51cto.com/13806592/2171700