0

我有 3 个问题,我无法解决。
存在 2 个表

CREATE TABLE T_DEPARTMENT
(
  ID    NUMBER,
  NAME  VARCHAR2(100 BYTE)

CREATE TABLE T_EMPLOYEE
(
  ID             NUMBER,
  DEPARTMENT_ID  NUMBER,
  CHIEF_ID       NUMBER,
  NAME           VARCHAR2(100 BYTE),
  SALARY         NUMBER
)

问题:

1 - 获取部门名称和其中的员工人数,员工人数超过20人

2 - 显示各部门在总预算中的消耗百分比

3 - 找出比该部门最高薪水的员工高出不到 10% 的主管薪水。

我尝试了这些,都是不正确的:

1.

select id from t_employee group by id having count(*) <= 20

2.

select sum (salary) from t_employee group by id

3.

select e1.name from t_employee e1, t_employee e2 where e2.id = e1.chief_id and e1.salary < e2.salary*0.1 all is uncorrect
4

2 回答 2

0

在 sql 服务器

1>

select a.id,a.name from T_DEPARTMENT a join T_EMPLOYEE b on a.ID=b.DEPARTMENT_ID group by a.id having count(*) >=20

2>

Select salary, (SUM(salary)* 100 / (Select Count(*) from T_EMPLOYEE)) as percentage_sal
    From T_EMPLOYEE
    Group By salary

3>

select e1.name  from T_DEPARTMENT e1, T_EMPLOYEE e2 where e2.id = e1.chief_id and e1.salary < e2.salary*0.1`
于 2013-10-21T06:42:55.693 回答
0

好的第一部分使用这个查询:

select 
t1.name,
count(t1.name) as EmployeesCount,
case when count(t1.name)>20 then 'True' else 'False' end as moreThan20 
from 
T_DEPARTMENT t1,
T_EMPLOYEE t2 
where 
t1.id = t2.department_id 
group by t1.name;

第二个:

select 
name,
salary,
(salary * 100) / (select sum(salary) from hatest2)  as percentage
from 
T_EMPLOYEE
group by salary;

最后我想你有一个表名作为 T_CHIEF 并且这个选择显示你不是主管的人员:

select 
t2.name  
from 
T_CHIEF t1,
T_EMPLOYEE t2 
where 
t1.id = t2.chief_id and 
t2.salary < (select max(salary) * 0.1 from T_EMPLOYEE where CHIEF_ID not in(select id from T_CHIEF)) ;
于 2013-10-21T08:06:54.333 回答