我正在使用Oracle。我有这样的表:
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;
我有两个问题:
(1)我今天无法访问oracle而无法测试它。请问我的代码是否正确?
(2)它看起来很复杂,有什么方法可以简化它吗?
答案 0 :(得分: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)
这应该是接近的。它也会产生联系。