测试脚本:
--创建表
CREATE TABLE #Temp
(
Id INT,
Name VARCHAR(50),
Score INT,
Time Date
)
--添加测试数据
INSERT INTO #Temp ( Id, Name, Score, Time )
VALUES ( 1, '张三',80,'2016-2-1')
,( 2, '张三',85,'2016-2-10')
,( 3, '李四',60,'2016-2-8')
,( 4, '李四',85,'2016-2-20')
答案一:
SELECT * FROM #Temp AS a
WHERE a.Id = ( SELECT TOP 1 Id FROM #Temp AS b WHERE a.Name = b.Name ORDER BY b.Time asc )
答案二:
select * from #Temp where Time in
(select min(Time) from #Temp group by Name)
SELECT * FROM #Temp AS a
WHERE a.Id = ( SELECT TOP 1 Id FROM #Temp AS b WHERE a.Name = b.Name ORDER BY b.Time asc )
答案二:
select * from #Temp where Time in
(select min(Time) from #Temp group by Name)