Concat记录SQL Server

时间:2015-07-06 12:19:57

标签: sql-server concatenation

我有这个查询,我不能让它连续第二列。

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  |
 -------------------------------

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

有人可以帮助我吗?

谢谢

2 个答案:

答案 0 :(得分:1)

您可以尝试以下代码:

-- 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

如果您想让它适应您的桌面结构,只需使用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)

答案 1 :(得分:0)

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

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