Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.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 server Concat记录SQL Server_Sql Server_Concatenation - Fatal编程技术网

Sql server Concat记录SQL Server

Sql server Concat记录SQL Server,sql-server,concatenation,Sql Server,Concatenation,我有这个查询,但无法连接到第二列 SELECT Container.UIDaughterPlateId AS UIDaughterPlateId, AllLastOperationInfo.OperationShortLabel AS lab FROM ((InSite.UIDaughterPlate AS Container INNER JOIN InSite.UIDaughterPlateInfo AS UIDaughterPlateInfo ON

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

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  |
 -------------------------------
我试过在其他帖子中找到的几个例子,但都没有成功

有人能帮我吗


谢谢

您可以尝试以下代码:

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

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

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

group_concat不是内置的SQL Server功能为什么选择无法在所需系统上运行的解决方案-我已经在下面给出了我的答案。你用它。它也包含演示数据。您只需使用注释中显示的
选择
。只需将
#temp
替换为您的表格即可。