Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 每个部门的集团员工_Sql_Sql Server_Grouping_Group Concat - Fatal编程技术网

Sql 每个部门的集团员工

Sql 每个部门的集团员工,sql,sql-server,grouping,group-concat,Sql,Sql Server,Grouping,Group Concat,我有一个表详细信息 DeptId EmpID ------- --------- 1 1 1 5 1 3 2 8 2 9 我想把他们分组如下: DeptId EmpIDs ------- ------- 1 1,5,3 2 8,9 我想在SQL Server中使用它。我知道这可以在MySQL中使

我有一个表
详细信息

DeptId        EmpID
-------      ---------
1             1
1             5
1             3
2             8
2             9
我想把他们分组如下:

DeptId      EmpIDs
-------    -------
 1          1,5,3
 2          8,9
我想在SQL Server中使用它。我知道这可以在MySQL中使用
Group\u Concat
函数完成。e、 g

SELECT DeptId, GROUP_CONCAT(EmpId SEPARATOR ',') EmpIDS
FROM Details GROUP BY DeptId


但是如何使用SQL Server实现这一点呢?我不知道任何函数。

SQLServer
中模拟
组CONCAT
的一种方法是对XML路径()使用
交叉应用

select a.[DeptId], SUBSTRING(d.detailsList,1, LEN(d.detailsList) - 1) detailsList
from 
  (
    SELECT DISTINCT [DeptId]
    FROM details
  ) a
CROSS APPLY
  (
    SELECT [EmpID] + ', ' 
    FROM details AS B 
    WHERE A.[DeptId] = B.[DeptId]
    FOR XML PATH('')
  ) D (detailsList)