稍微简化一下:
SELECT NAME,
SUM(CASE WHEN MAJOR = '数学' THEN SCORE ELSE 0 END)数学 ,
SUM(CASE WHEN MAJOR = '外语' THEN SCORE ELSE 0 END)外语 ,
SUM(CASE WHEN MAJOR = '语文' THEN SCORE ELSE 0 END)语文 ,
AVG(SCORE)
FROM STUDENT GROUP BY NAME
having SUM(CASE WHEN SCORE > 80 THEN 1 ELSE 0 END) >= 2
AND AVG(SCORE) >80
SELECT id,
SUM(CASE WHEN major = '数学' THEN score ELSE 0 END) ,
SUM(CASE WHEN major = '外语' THEN score ELSE 0 END) ,
SUM(CASE WHEN major = '语文' THEN score ELSE 0 END) ,
AVG(score)
FROM student
WHERE student.id IN (SELECT id FROM student GROUP BY id HAVING SUM(CASE WHEN score > 80 THEN 1 ELSE 0 END) >= 2)
GROUP BY id HAVING AVG(score) > 80
即可
对的吧