今天碰到一个需求,按GroupId分组后,取UploadTime最早的数据,下面是我憋了半小时才憋出来的sql,数据库是mssql,不支持子查询中使用order by(除非另外还指定了TOP或FOR XML)

1
select t1.* from Question t1 inner join (SELECT min(UploadTime) as UploadTime, GroupId FROM Question group by GroupId) t2 on t1.UploadTime=t2.UploadTime and t1.GroupId=t2.GroupId

发现我的sql是真的菜,要好好学习学习了