迷你任务是让我列出每个部门的所有员工,按部门分组,并根据最大年龄选择每个部门的领导。
我只能提取每个部门的最低出生日期,但我不能提取部门中最老的家伙的名字和姓氏。我写了这一行:
SELECT dno,COUNT(*),MIN(bdate) FROM employees GROUP BY dno;
我不知道如何制作它,以便它不会出生日期,而是写出生日最低的雇员的名字和姓氏。
你能帮帮我吗?我学得很快,但我不太懂手册。我猜我只需要使用HAVING功能。答案 0 :(得分:4)
select e.dno, e.fname, e.lname, em.Count
from employees e
inner join (
select dno, min(bdate) as minbdate, count(*) as Count
from employees
group by dno
) em on e.dno = em.dno and e.bdate = em.minbdate
答案 1 :(得分:1)
select e.dno, total, e.bdate, fname, lname
from (
SELECT
dno,
COUNT(*) as total,
MIN(bdate) as bdate,
from employees
GROUP BY dno
) s
inner join
employees e on e.dno = s.dno and e.bdate = s.bdate
order by dno