使用HR表中AVG表达式的条件选择数据

时间:2015-06-30 11:47:35

标签: sql oracle

我尝试学习mysql oracle数据库,我想通过department_id显示表员工组的平均工资,平均工资是'%0'。 这是我的查询

select department_id, AVG(salary) as "Pay Salary per dept" 
from employees 
where department_id is not null and AVG(salary) like '%0'
group by department_id order by department_id

什么是正确的查询替换

AVG(salary) like '%0' ?

由于

2 个答案:

答案 0 :(得分:1)

使用MOD功能代替LIKE

where MOD(salary,10)=0

<强>查询

select t.dept_id,t.Pay_Salary_per_dept
from
(
   select dept_id, AVG(salary) as Pay_Salary_per_dept
   from employee 
   where dept_id is not null
   group by dept_id
)t
where mod(t.Pay_Salary_per_dept,10)=0
order by t.dept_id;

Fiddle demo here

答案 1 :(得分:0)

要过滤聚合函数的结果,SQL中有HAVING子句。 您可以在没有子查询的情况下直接编写它,如下所示:

select dept_id, AVG(salary) as Pay_Salary_per_dept
from employee 
where dept_id is not null
group by dept_id
having mod(AVG(salary),10)=0
;