烙印99
嗯......我看到答案已被接受......但我认为你应该看到另一种解决方案:/* EXAMPLE */DECLARE @UserAliases TABLE(UserId INT , Alias VARCHAR(10))INSERT INTO @UserAliases (UserId,Alias) SELECT 1,'MrX' UNION ALL SELECT 1,'MrY' UNION ALL SELECT 1,'MrA' UNION ALL SELECT 2,'Abc' UNION ALL SELECT 2,'Xyz'/* QUERY */;WITH tmp AS ( SELECT DISTINCT UserId FROM @UserAliases )SELECT LEFT(tmp.UserId, 10) + '/ ' + STUFF( ( SELECT ', '+Alias FROM @UserAliases WHERE UserId = tmp.UserId FOR XML PATH('') ) , 1, 2, '' ) AS [UserId/Alias]FROM tmp/* -- OUTPUT UserId/Alias 1/ MrX, MrY, MrA 2/ Abc, Xyz */