我刚刚学习了一个月的SQL,还有许多我仍然无法掌握的东西。任何人都可以帮助我,PLZ提供一些解释的结果,我需要了解它以供将来使用。我只列出了一些我无法理解的内容。
我能做到:
SELECT employeeNumber
FROM employees
WHERE jobTitle LIKE '%Manager%'
UNION
SELECT employeeNumber, COUNT(*)
FROM employees
WHERE reportsTo 'WHICH CONDITION?'
ORDER BY COUNT(*) DESC
有人可以在'reportTo'之后填写,我找不到一个有效的条件TYT
我能做到:
SELECT country, COUNT(*)
FROM offices
GROUP BY country
UNION
SELECT country, officeCode
FROM offices
但结果并不像预期的那样
答案 0 :(得分:0)
select
reportsTo,
COUNT(employeeNumber) as numberOfEmployees
from employees
group by reportsTo
将为您提供向该报告报告的employeeNumbers的计数。 这不会给你没有人报告的经理,所以要做到这一点你必须加入:
SELECT
a.employeeNumber AS managerNumber,
COUNT(b.employeeNumber) AS numberOfEmployees
FROM employees AS a
LEFT JOIN employees AS b on (b.reportsTo=a.employeeNumber)
WHERE a.jobTitle LIKE '%Manager%'
GROUP BY a.employeeNumber