1

我正在尝试编写一个查询来查找 AdventureWorks 数据库中男性和女性员工的平均工资率

我写了这个(如下),但我没有得到想要的结果:

with sub as 
(
   select 
       emp.Gender, emp.VacationHours, pay.Rate
   from 
       HumanResources.Employee emp, HumanResources.EmployeePayHistory pay
   where 
       emp.BusinessEntityID = pay.BusinessEntityID
)
select 
    sub.Gender,
    avg(sub.VacationHours) as vac_hours, 
    avg(sub.Rate) as rate
from 
    sub
group by 
    sub.Gender, Rate;

我正在尝试这样做,以便更好地了解函数的工作原理

4

2 回答 2

2

主要问题是你正在对rate你平均的分组进行分组 - 不要那样做。此外,公用表表达式并没有真正填充任何函数,因此也可以将其删除:

select 
    Gender,
    avg(VacationHours) as vac_hours, 
    avg(Rate) as rate
from 
    HumanResources.Employee emp
join
    HumanResources.EmployeePayHistory pay on emp.BusinessEntityID = pay.BusinessEntityID
group by 
    Gender;
于 2015-04-26T19:00:15.120 回答
1

仅按gender单独分组 - 而不是按性别和比率:

with sub AS
(
   select 
       emp.Gender, emp.VacationHours, pay.Rate
   from 
       HumanResources.Employee emp
   inner join 
       HumanResources.EmployeePayHistory pay on emp.BusinessEntityID = pay.BusinessEntityID
)
select 
    sub.Gender,
    avg(sub.VacationHours) as vac_hours, 
    avg(sub.Rate) as rate
from 
    sub
group by 
    sub.Gender;
于 2015-04-26T18:51:24.000 回答