0

I realize i'm far off the solution with what i have:

Select FirstName || ' ' || LastName AS Manager From Employee 
Where (Select COUNT(ReportsTo) from Employee 
group by ReportsTo 
order by ReportsTo desc);

ReportsTovalues are the EmployeeID they report to

What i want is to query the name of the employee with the most Employees reporting to them and who they in turn report to without nulls. I'm Not sure how to make the connections between columns values such as ReportsTo to EmployeeID so any explanation would help

For Example the output i would want is two columns say | Fred Jones | Mary Anne| the first being the employee with the most reportsTo with the same value as their EmployeeID and the second being the name of the employee with the same EmployeeID as the first employees ReportTo

2
  • can you rephrase your question. It's not very obvious what you want to achieve, maybe an example would help.
    – Yahya
    May 18, 2016 at 9:30
  • Edited it , i hope this clears it up somewhat May 18, 2016 at 9:37

2 Answers 2

1

Do this step by step:

First step: Count how many employees report to a person.

select reportsto, count(*) from employee group by reportsto;

We can order this result by count(*) and limit it to only get one row, so as to get the person with the most reporters. Only problem is: What to do in case of ties, i.e. two persons have the same highest amount of reporters? SQLite doesn't offer much to help here. We'll have to query twice:

select reportsto
from employee 
group by reportsto 
having count(*) =
(
  select count(*) 
  from employee 
  group by reportsto 
  order by count(*) desc
  limit 1
);

Next step: Get the name. That means we must access the table again.

select 
  firstname || ' ' || lastname as manager
from employee
where e1.employeeid in
(
  select reportsto
  from employee 
  group by reportsto 
  having count(*) =
  (
    select count(*) 
    from employee 
    group by reportsto 
    order by count(*) desc
    limit 1
  )
);

Last step: Get the persons our found managers themselves report to. These can be many, so we group by manager and concatenate all those they report to.

select 
  e1.firstname || ' ' || e1.lastname as manager,
  group_concat(e2.firstname || ' ' || e2.lastname) as reportsto
from employee e1
join employee e2 on e2.employeeid = e1.reportsto
where e1.employeeid in
(
  select reportsto
  from employee 
  group by reportsto 
  having count(*) =
  (
    select count(*) 
    from employee 
    group by reportsto 
    order by count(*) desc
    limit 1
  )
)
group by e1.firstname || ' ' || e1.lastname;
1
  • Thanks, this is exactly what i was looking for. May 18, 2016 at 10:15
-1
SELECT e.ReportsTo AS TopManagersEmployeeId, COUNT(e.ReportsTo) AS ReportedBy, m.FirstName + ' ' + m.LastName AS TopManagersName, mm.FirstName + ' ' + mm.LastName AS TheirManagersName FROM Employees e
JOIN Employees m
ON e.ReportsTo = m.EmployeeID
JOIN Employees mm
ON m.ReportsTo = mm.EmployeeID
GROUP BY e.ReportsTo,  m.FirstName, m.LastName, mm.FirstName, mm.LastName 

Once you have this data, you can do TOP 1 etc. You can also play around with JOIN, and make it INNER JOIN in the second set where Manager's Manager (mm) is being retrieved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.