create table tb ( 自动编号 int identity(1,1), 姓名 nvarchar(50), 时间 nvarchar(50) )insert into tb select '张三', '06:50' union all select '张三', '07:50' union all select '张三', '07:59' union all select '李四', '06:50' union all select '李四', '06:56' union all select '王五', '06:50' union all select '王五', '08:50'select * from tb a where not exists ( select 1 from tb b where b.姓名=a.姓名 and a.时间 < b.时间 )drop table tb 结果: 3 张三 07:59 5 李四 06:56 7 王五 08:50
WITH Test AS ( SELECT 姓名 ,MAX(时间)AS 最大时间 FROM TableName GROUP BY 姓名 ) SELECT a.* FROM TableName AS a INNER JOIN Test AS b ON a.姓名=b.姓名 AND a.时间=b.最大时间
create table tb
(
自动编号 int identity(1,1),
姓名 nvarchar(50),
时间 nvarchar(50)
)insert into tb
select '张三', '06:50' union all
select '张三', '07:50' union all
select '张三', '07:59' union all
select '李四', '06:50' union all
select '李四', '06:56' union all
select '王五', '06:50' union all
select '王五', '08:50'select * from tb a
where not exists
(
select 1 from tb b where b.姓名=a.姓名 and a.时间 < b.时间
)drop table tb
结果:
3 张三 07:59
5 李四 06:56
7 王五 08:50
(
SELECT 姓名 ,MAX(时间)AS 最大时间
FROM TableName
GROUP BY 姓名
)
SELECT a.*
FROM TableName AS a
INNER JOIN Test AS b
ON a.姓名=b.姓名 AND a.时间=b.最大时间