*/
--------------- 2.
SELECT
avg_score,studentname
FROM (
SELECT
AVG(score) AS avg_score
,studentname
FROM records
GROUP BY studentname
) AS a
WHERE NOT EXISTS(SELECT 1 FROM (
SELECT
AVG(score) AS avg_score
,studentname
FROM records
GROUP BY studentname
) AS x
WHERE x.avg_score>a.avg_score
)
/*
avg_score studentname
----------- -----------
58 p2
*/
------解决方案--------------------CREATE VIEW V_ChengJiDan
AS
SELECT a.studentname,b.* FROM dbo.records a
INNER JOIN
(
SELECT subject,MAX(score) score
FROM records
GROUP BY subject
) b
ON a.score=b.score
GO
SELECT a.studentname,b.* FROM dbo.records a
INNER JOIN
(
SELECT TOP(1) subject,AVG(score) score
FROM records
GROUP BY subject
ORDER BY score DESC
) b
ON a.score=b.score
------解决方案--------------------SELECT * FROM records a WHERE NOT EXISTS (SELECT 1 FROM records WHERE [subject]=a.[subject] AND score>a.score)
SELECT TOP 1 WITH ties studentname,AVG(score) FROM records GROUP BY studentname ORDER BY AVG(score) desc