2015-02-05 64 views
0

我正在使用Oracle。我有表所示:STDDEV函數和條款

Company Employee salary 
    A1  Jim  122000 
... 

我想和其薪水超過2點標準差(〜> 95%)的員工數最高回報的公司。這裏是我的代碼

With Com_2Std as (
Select company-name, AVG(salary)+2*STDDEV(salary) as AboveS 
From works 
Group By company-name) 

Select company-name, count(employee-name) as ENumber 
From works 
Where ENumber=MAX(
Select count(a.employee-name) 
From works a, Com_2Std b 
Where a.company-name=b.company-name 
And a.salary>b.AboveS; 
Group by a.company-name) 
Group by company-name; 

我有兩個quesions:

(1)我無法訪問Oracle今天不能對其進行測試。請問我的代碼是否正確? (2)看起來相當複雜,有什麼辦法可以簡化它嗎?

+0

不,你不能這樣使用MAX()。我不願意爲你寫一個Oracle版本,因爲我不太熟悉它的某些語法特點。 – shawnt00 2015-02-05 22:05:08

回答

1
with Com_2Std as (
    select company-name, AVG(salary)+2*STDDEV(salary) as AboveS 
    from works 
    group by company-name 
), 
CompanyCount as (
    select a.company-name, count(*) as CountAboveS 
    from 
     works a 
     inner join Com_2Std b on a.company-name=b.company-name 
    where 
     a.salary > b.AboveS 
    group by a.company-name 
) 
select company-name 
from CompanyCount 
where CountAboveS = (select max(CountAboveS) from CompanyCount) 

這應該是接近的。它也會產生聯繫。

+0

謝謝。這非常有幫助。 – user4441082 2015-02-05 22:14:06