Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum of averages

I'm supposed to get every departments average wage and only show the department with the highest average wage. I figured out this query, but it doesn't work. Anyone got some ideas?

SELECT department, max(avg(wage))
FROM employees
GROUP BY department;

I get this error: ERROR at line 1: ORA-00937: not a single-group group function

like image 845
Bill Gates Avatar asked Mar 13 '10 19:03

Bill Gates


1 Answers

Without CTEs you can do:

Select Z.Department, Z.AvgWage
From  (
        Select Department, Avg(Wage) AvgWage
        From Employees
        Group By Department
        ) As Z
Where AvgWage = (
                Select Max(Z1.AvgWage)
                From    (
                        Select Department, Avg(Wage) AvgWage
                        From Employees
                        Group By Department
                        )  Z1
                )

With CTEs you could do:

With AvgWages As
    (
    Select Department
        , Avg(Wage) AvgWage
        , Rank() Over( Order By Avg(Wage) Desc ) WageRank
    From Employees
    Group By Department
    )
Select Department, AvgWage, WageRank
From AvgWages
Where WageRank = 1
like image 179
Thomas Avatar answered Oct 14 '22 09:10

Thomas