0

的SQL Server 2014逗号分隔值连接我在SQL Server 2014的两个表:唯一值与每个ID

  1. current_entry
  2. data_by_client

我有两列User_Skill 。我需要独特的Skill值来根据用户ID从select语句中的两个表中连接起来。 String_Agg在SQL Server 2014中不起作用,所以我尝试使用XML PATH。

请找的预期产出的附截图:

enter image description here

这里是SQL捣鼓它:SQL Fiddle

我用下面的XML路径查询:

SELECT 
    User_, Products + Skill 
FROM 
    (SELECT DISTINCT 
     User_, Skill, 
     (SELECT DISTINCT rs.Skill + ',' 
      FROM dbo.current_entry u 
      INNER JOIN dbo.data_by_client rs ON u.User_ = rs.User_ 
      WHERE rs.User_ = r.User_ 
      FOR XML PATH('')) AS Products 
    FROM  
     dbo.current_entry r) l 

我正面临着问题,因为我没有得到预期的输出。

回答

0

您可以尝试使用foll来锻炼您的解决方案。方法:

SELECT 
    t1.user, 
    Item = stuff((SELECT (', ' + skill) 
         FROM #string_test t2 
         WHERE t1.user = t2.user 
         ORDER BY 1 
         FOR XML PATH('') 
        ), 1, 1, '')FROM #string_test t1 
GROUP BY user 

参考:https://social.msdn.microsoft.com/Forums/sqlserver/en-US/4ac19fe5-be05-479f-bcc2-8fdfa6c1ae1f/pivot-string-concatenation?forum=transactsql

然后,您可以使用自定义的SQL函数删除重复。参考http://blog.sqlauthority.com/2009/01/15/sql-server-remove-duplicate-entry-from-comma-delimited-string-udf/amp/

+0

nope ..不工作... – user1538020