1. 题目 :
自己解答 :
WITH temp -- 查出每一个系每个人的总分 AS( SELECT StuId, dep.DepId, dep.DepName,StuName, (SELECT SUM(Score) FROM Score WHERE StuId=stu.StuId) sumScore FROM Stu INNER JOIN Dep on Dep.DepId=stu.DepId ) SELECT * FROM temp a INNER JOIN (SELECT DepId, max(sumScore) maxScore FROM temp GROUP BY DepId) ms -- 以系分组取出每系最高分 ON ms.DepId=a.DepId AND ms.maxScore=a.sumScore -- 以系id和总分查出学员信息 ORDER BY a.DepId