如何显示员工人数第二高的项目名称?
关系模式,具有主键和外键(适用时):
Employee(Eid,Ename,Address,city,Doj,salary) Project(Pid,Location,Pname,Mng,Client,Branch)
Works(Eid,Pid)
Eid和Pid in Works是ForeignKey。 eid和pid分别是员工和项目中的主键
select count (*) , pname
from project natural join work
group by pname;
但这只会提供根据项目的员工人数...而不是最大员工人数
select max as ex1
from (Select count(*) as ex1
from works
where PID in ( select distinct pid from works);
我正在linux-oracle中尝试
答案 0 :(得分:1)
SELECT COUNT(W.Eid) AS TotalEmployees, P.pname
FROM Works W
INNER JOIN Project P ON W.Pid = P.Pid
GROUP BY P.Pid
ORDER BY TotalEmployees desc
OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLY
答案 1 :(得分:0)
好的,我认为下面的SQL将返回您想要的内容(2号项目的名称和员工编号):
select * from
(
select
p.pname,
count(1) as cc
from
work w
join
project p on w.pid = p.pid
join
employee e on w.eid = e.eid
group by
p.pname
order by cc desc limit 2
) tmp order by cc asc limit 1