简单的SQL查询无法解决

时间:2018-04-22 20:51:58

标签: sql

click here for database model

被问到:展示每个至少有3名员工的部门,部门的名称以及1967年以前出生的该部门的员工数量。

到目前为止我的代码:

`Select department, department_name, numberofemployeesbefore1967 = ( select count(empleyee_id) from employee where year(dateofbirth) < 1967)
From employee inner join department on (department = department_id)
group by department, department_name
having count(*) >=3`

我现在的输出:output

我觉得这是一个非常简单的问题,但我找不到如何只展示1967年之前为该特定部门出生的员工。 有人帮帮我吗?

2 个答案:

答案 0 :(得分:0)

像这样调整子查询......

Select department, department_name, (select count(empleyee_id) from employee where year(dateofbirth) < 1967) AS numberofemployeesbefore1967 From employee inner join department on (department = department_id) group by department, department_name having count(*) >=3

答案 1 :(得分:0)

我在主查询中检查年份,在子查询中检查&gt; = 3

SELECT department department_name, count(*)
FROM department d
JOIN employee e on d.department_id = e.department
WHERE YEAR(dateOfBirth) < 1967 
  AND (SELECT COUNT(*) FROM employee WHERE department = d.department_id) >= 3
GROUP BY d.id, d.name