我想在SQL中将MIN()
和AVG()
函数分组,即
MIN(AVG(column_name))
有办法进行分组吗?
我尝试了这个:
SELECT
`instructer`.`dept_name`, AVG(`instructer`.`salary`)
AS
MinAvgSalary
FROM
`instructer`
GROUP BY
`instructer`.`dept_name`
HAVING
AVG(`instructer`.`salary`) = (SELECT MIN(AVG(`instructer`.`salary`)) FROM `instructer` GROUP BY `instructer`.`dept_name`)
但是MySQL说:这是我的讲师表:
最佳答案
您可以使用子选择
select t.dept_name, min(t.AvgSalary) AS MinAvgSalary
from (
SELECT
`instructer`.`dept_name` AS dept_name
, AVG(`instructer`.`salary`) AS AvgSalary
FROM `instructer`
GROUP BY `instructer`.`dept_name` ) t
GROUP BY t.dept_name
HAVING t.AvgSalary = min(t.AvgSalary)
关于mysql - 有没有一种对SQL函数进行分组的方法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39773689/