【发布时间】:2019-02-20 02:46:01
【问题描述】:
有两张桌子
Employee
Id
Name
Salary
DepartmentId
和
Departament
Id
Name
如何才能得到两张表中最高的平均工资 喜欢
Joe and Max belong to dept 1 so, avg is (70K+90K)/2
= 80K
和
Henry and Sam belog to dept 2, avg is (80K + 60K)/2=70k
那么如何通过部门选择最高的平均工资?,在这种情况下
IT 80K
我一直在尝试:
'按部门对工资进行分组,用Max函数取最高的。
select
Department.Name as Department,
T.M as Salary
from
Employee,
Department,
(select DepartmentId as ID, Max(Salary) as M from Employee group by DepartmentId) as T
where
Employee.Salary = T.M and
Department.Id = T.ID and
Employee.DepartmentId = Department.Id
【问题讨论】:
-
您也可以尝试使用 Avg 来代替 Max 吗?您使用的是哪个 RDBMS?
-
我使用的是sql server
-
Bad habits to kick : using old-style JOINs - 旧式 逗号分隔的表格列表 样式已替换为 ANSI 中的 proper ANSI
JOIN语法-92 SQL 标准(25 多年前),不鼓励使用它
标签: sql sql-server