【问题标题】:Concat records SQL ServerConcat 记录 SQL Server
【发布时间】:2015-07-06 12:19:56
【问题描述】:

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

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

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

有人可以帮我吗?

谢谢

【问题讨论】:

  • 我在下面提供了我的答案。你用吧。它也包含演示数据。您只需使用在 cmets 中看到的 SELECT。只需将 #temp 替换为您的表格即可。

标签: sql-server concatenation


【解决方案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)

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • group_concat 不是 SQL Server 内置函数
    • 为什么对无法在所需系统上运行的解决方案进行投票? :-D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    相关资源
    最近更新 更多