Concat 记录 SQL 服务器

Concat records SQL Server

我有这个查询,但无法连接第二列。

SELECT 
    Container.UIDaughterPlateId AS UIDaughterPlateId, 
    AllLastOperationInfo.OperationShortLabel AS lab
FROM 
    ((InSite.UIDaughterPlate AS Container 
INNER JOIN 
    InSite.UIDaughterPlateInfo AS UIDaughterPlateInfo ON (Container.UIDaughterPlateInfoId = UIDaughterPlateInfo.UIDaughterPlateInfoId ))
LEFT OUTER JOIN 
    (SELECT 
         UIOperationInfo.UIOperationInfoId as OperationInfoId,
         UIOperationInfo.ParentId as DaughterPlateInfoId,   
         UIOperationInfo.Status as Status,  
         Operation.ShortLabel as OperationShortLabel, 
         UIOperationInfo.IsLast 
     FROM
         UIOperationInfo
     INNER JOIN
         Operation ON Operation.OperationId = UIOperationInfo.UIOperationId
     WHERE 
         UIOperationInfo.IsLast = 1 and Status = 'A Réaliser') AllLastOperationInfo ON (UIDaughterPlateInfo.UIDaughterPlateInfoId = AllLastOperationInfo.DaughterPlateInfoId )) 
ORDER BY 
    Container.UIDaughterPlateName DESC

目前的结果是

 -------------------------
| UIDaughterPlateId | Lab |
|-------------------------|
|   42              | MD  |
|   42              | MC  |
|   47              | MC  |
|   67              | MA  |
|   67              | MB  |
|   67              | MC  |
 -------------------------

我想要这些结果

 -------------------------------
| UIDaughterPlateId |    Lab    |
|-------------------------------|
|   42              | MC MD     |
|   47              | MC        |
|   67              | MA MB MC  |
 -------------------------------

我尝试了在其他 post 中找到的几个示例,但没有成功。

有人可以帮助我吗?

谢谢

您可以试试下面的代码:

-- Create demo data
CREATE TABLE #temp(UIDaughterPlateId int, Lab nvarchar(5)) 

INSERT INTO #temp(UIDaughterPlateId, Lab)
VALUES  (42,N'MD'),(42, N'MC'),(47, N'MC'),(67, N'MA'),(67, N'MB'),(67, N'MC')

-- Your part:
SELECT DISTINCT t.UIDaughterPlateId, LEFT(dat.Lab,LEN(dat.Lab)-1) as Lab
FROM #temp AS t
OUTER APPLY (
    SELECT s.Lab+N', '
    FROM #temp as s
    WHERE s.UIDaughterPlateId = t.UIDaughterPlateId
    FOR XML PATH(N'')
) as dat(Lab)

-- Cleanup
DROP TABLE #temp

在给定的输入上:

UIDaughterPlateId Lab
----------------- -----
42                MD
42                MC
47                MC
67                MA
67                MB
67                MC

这是查询的结果:

UIDaughterPlateId Lab
----------------- ----------
42                MD, MC
47                MC
67                MA, MB, MC

如果您想使其适应您的 table 结构,只需为此使用 CTE

WITH data AS(
    -- your code from your question
    SELECT 
        Container.UIDaughterPlateId AS UIDaughterPlateId, 
        AllLastOperationInfo.OperationShortLabel AS lab
    FROM 
        ((InSite.UIDaughterPlate AS Container 
    INNER JOIN 
        InSite.UIDaughterPlateInfo AS UIDaughterPlateInfo ON (Container.UIDaughterPlateInfoId = UIDaughterPlateInfo.UIDaughterPlateInfoId ))
    LEFT OUTER JOIN 
        (SELECT 
             UIOperationInfo.UIOperationInfoId as OperationInfoId,
             UIOperationInfo.ParentId as DaughterPlateInfoId,   
             UIOperationInfo.Status as Status,  
             Operation.ShortLabel as OperationShortLabel, 
             UIOperationInfo.IsLast 
         FROM
             UIOperationInfo
         INNER JOIN
             Operation ON Operation.OperationId = UIOperationInfo.UIOperationId
         WHERE 
             UIOperationInfo.IsLast = 1 and Status = 'A Réaliser') AllLastOperationInfo ON (UIDaughterPlateInfo.UIDaughterPlateInfoId = AllLastOperationInfo.DaughterPlateInfoId )) 
    ORDER BY 
        Container.UIDaughterPlateName DESC
)
SELECT DISTINCT t.UIDaughterPlateId, LEFT(dat.Lab,LEN(dat.Lab)-1) as Lab
FROM data AS t
OUTER APPLY (
    SELECT s.Lab+N', '
    FROM data as s
    WHERE s.UIDaughterPlateId = t.UIDaughterPlateId
    FOR XML PATH(N'')
) as dat(Lab)

使用当前结果创建视图A,然后执行

SELECT UIDaughterPlateId, replace(group_concat(lab),',',' ') AS lab FROM A GROUP BY UIDaughterPlateId