用T-sql语句写出 查询出平均分大于80分,且至少两门课大于80的学生。 表如下:

2024-12-03 07:31:28
推荐回答(3个)
回答1:

稍微简化一下:
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

回答2:

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

即可

回答3:

对的吧