I am struggling with a query to return a list of managers with their respective employees
I have three tables as follows:
Managers
ManagerID   ManagerName
1           Bob
2           Sally
3           Peter
4           George
EmployeeManager
EmployeeID  ManagerID
1           1
1           1
2           2
2           2
3           3
3           3
4           4
4           4
Employees
EmployeeID  EmployeeName
1           David
1           Joseph
2           Adam
2           Pete
3           Mark
3           Mavis
4           Susan
4           Jennifer
Desired Result Set
ManagerName CountEmployee   Employees
Bob          2              David, Joseph
Sally        2              Anish, Pete
Peter        2              Mark, Mavis
George       2              Susan, Jennifer
The query I am currently using is as follows:
Select m.ManagerName 
        ,Count(e.EmployeeName) Over(Partition By m.ManagerID) as CountEmployee 
        ,Rank() Over(Partition By m.ManagerID Order By em.EmployeeID) [RankEmployee]
       ,e.EmployeeName
From dbo.Employees e
Left Join dbo.EmployeeManager em on em.ManagerID=e.ManagerID
Left Join dbo.Managers m on m.ManagerID=em.ManagerID;
This returns a list of managers and employees on individual rows but I'm struggling to concatenate the employee names as per the above table.
Any ideas or solutions?
Manpaal Singh
 
     
     
     
    