2015-12-01 68 views

回答

0

我試圖創建表:

mysql> SELECT * FROM emp; 
+------+--------+-------+ 
| job | deptno | sal | 
+------+--------+-------+ 
| SSD |  1 | 58000 | 
| SD |  1 | 20000 | 
| GD |  1 | 35000 | 
| AO |  2 | 40000 | 
| AhO |  2 | 25000 | 
| AhO |  2 | 55000 | 
| SSD |  1 | 68000 | 
+------+--------+-------+ 
7 rows in set (0.00 sec) 

我運行查詢,所以我就:

mysql> select max(avg(sal)) from emp group by job; 
ERROR 1111 (HY000): Invalid use of group function 

那是因爲你有兩個嵌套聚合函數。

爲了解決你的問題,我已經改變了你的查詢,如下所示:

SELECT 
    job, 
    AVG(sal) AS average_salary 
FROM emp 
GROUP BY job 
ORDER BY average_salary DESC 
LIMIT 1; 

+------+----------------+ 
| job | average_salary | 
+------+----------------+ 
| SSD |  63000.0000 | 
+------+----------------+ 
+0

感謝您的回覆。我只是想知道它的最高平均工資是不是平均工資。我希望它是正確的,謝謝 – user5623978