我有这样的SQL数据库,其中有员工和主管分配给他们。对于没有为其工作的男性员工的主管,我需要找到SSN和为他们工作的员工总数。
这是我的解决方案代码:
SELECT supervisors.ssn, COUNT(e.ssn)
FROM employee AS supervisors, employee AS e
WHERE e.ssn NOT IN (SELECT e.ssn FROM employee e WHERE e.sex = 'M') AND supervisors.ssn = e.superssn
GROUP BY supervisors.ssn;
但上面的代码找到了至少有一名女性员工的主管。但是,我需要找到仅女性员工的主管。
答案 0 :(得分:1)
您的查询会返回至少有一名女性员工的所有主管。
如果您改变了对自己想要的方式的看法,那么很容易获得结果。
您想要一个查询,该查询将返回所有不属于至少有一名男性员工的主管成员的主管。
答案 1 :(得分:1)
如果主管是男性,许多答案将排除结果
这不会
SELECT employee.lname, count(employee.lname) as NumberOfEmployees
FROM Employee
INNER JOIN (
SELECT Employee.SuperSSN
FROM Employee
WHERE Employee.SuperSSN NOT IN
(SELECT Employee.SuperSSN FROM Employee e
INNER JOIN Employee
ON e.SSN = Employee.SuperSSN
WHERE Employee.SEX = 'M')
)
AS e
ON Employee.ssn = e.superssn
GROUP BY employee.lname
答案 2 :(得分:0)
这样的事情?
SELECT supervisors.ssn
FROM employee AS supervisors
inner join employee e on supervisors.ssn = e.supersnn
and e.sex = 'F'
group by supervisors.ssn