我需要写一个查询来列出所有雇员的姓名(在1列中)和他们所监督的个人人数。
我已经尝试过将计数与concat结合使用,但是它不起作用,我只是不知道如何继续前进。
select
concat(Boss.firstName, ' ', Boss.lastName) as 'Boss',
concat(supervised.firstName, ' ',supervised.lastName) as supervised,
count(supervised.firstName, ' ',supervised.lastName)
from Employees as Boss
left join Employees as supervised on Boss.employeeNumber=supervised.reportsTo;
我需要第二列具有数值。我会为此提供任何帮助!
答案 0 :(得分:1)
您需要在查询中添加分组并计算每个Boss的行数:
select
concat(Boss.firstName, ' ', Boss.lastName) as Boss,
count(supervised.lastName) counter
from Employees as Boss left join Employees as supervised
on Boss.employeeNumber = supervised.reportsTo
group by concat(Boss.firstName, ' ', Boss.lastName)
答案 1 :(得分:0)
您可以使用相关子查询。
SELECT concat(e1.firstname, ' ', e1.lastname) supervisor,
(SELECT count(*)
FROM employees e2
WHERE e2.reportsto = e1.employeenumber) supervised_count
FROM employees e1;