2015-06-30 22 views
1

我嘗試學習mysql oracle數據庫,我想通過department_id顯示錶員工組的平均工資,以及平均工資類似於'%0'的情況。 這裏是我的查詢從HR表中的AVG表達式中選擇數據使用條件

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' ? 

感謝

+0

你使用的是MySQL還是Oracle?請適當標記問題。另外,你的問題很簡單,無效:在數字上使用「like」是沒有意義的,特別是非整數。 –

+0

使用oracle,你能舉一些例子來回答嗎? '像''從我的情況整數 –

回答

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

+0

仍然沒有工作,結果顯示3475.55556 –

+0

@FatchulAmin:請檢查我更新的答案和小提琴。 – Wanderer

+0

謝謝,它的工作 –

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 
;