我有一個查詢努力回報經理的列表以及它們各自的員工T-SQL - 連接多個行
我有三個表如下:
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
所需的結果集
ManagerName CountEmployee Employees
Bob 2 David, Joseph
Sally 2 Anish, Pete
Peter 2 Mark, Mavis
George 2 Susan, Jennifer
我目前使用的查詢如下:
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;
這將返回單個行上的經理和員工列表,但我努力按照上表連接員工姓名。
任何想法或解決方案?
Manpaal辛格
搜索此:**在'SQL SERVER'模擬'GROUP_CONCAT' ** –
@msinghm可以的東西,結果以逗號分隔的結果:http://stackoverflow.com/a/18870585/6812070 –