数据库新手。我的架构如下:
Employee(**eid**, pname, age)
Dept(**dname**, num_managers) / Dept = department
Dept_Wing(**wingno**, wing_name, dname) / Dept_Wing = which wing of department
Wing_Sub_Division(**dname**, **wingno**, **sub_div_no**) / Wing_Sub_Division = which sub-division of the wing
On_Duty(**eid**, **dname**, **wingno**, sub_div_no)
我想得到:dname,wingno,这个翼的值班人员 对于每一个和所有的机翼,员工总数少于15%的员工(因此这包括有0名员工值班的dept_wings)。
我试过
select w.wingo, od.wingo
from dept_wing w left outer join on_duty od on on w.wingo=od.wingno
只是为了得到所有没有值班员工的翅膀,但我甚至无法归还那些翅膀!任何指导赞赏!
答案 0 :(得分:0)
您似乎正在使用复合键;部门机翼由dname
和wingno
标识,因此您可以使用Dept_Wing
,Wing_Sub_Division
和On_Duty
。这使得这个查询非常简单。
以下是如何获得每个机翼的值班员工数量:
select dname, wingno, count(*)
from on_duty
group by dname, wingno
所以整个查询都是
select
wing.dname, wing.wingno, coalesce(duty.cnt, 0) as employees_on_duty
from dept_wing wing
left join
(
select dname, wingno, count(*) as cnt
from on_duty
group by dname, wingno
) duty on duty.dname = wing.dname and duty.wingno = wing.wingno
where coalesce(duty.cnt, 0) < 0.15 * (select count(*) from employee);