这个问题本质上是Concatenate many rows into a single text string?重复。
这里有一个方法,在SQL Server 2008中的工作原理:
WITH Ranked (countryID, rnk, stateName)
AS (SELECT countryID,
ROW_NUMBER() OVER(PARTITION BY countryID ORDER BY countryID),
CAST(stateName AS VARCHAR(8000))
FROM state_master),
AnchorRanked (countryID, rnk, stateName)
AS (SELECT countryID, rnk, stateName
FROM Ranked
WHERE rnk = 1),
RecurRanked (countryID, rnk, stateName)
AS (SELECT countryID, rnk, stateName
FROM AnchorRanked
UNION ALL
SELECT Ranked.countryID, Ranked.rnk,
RecurRanked.stateName + ', ' + Ranked.stateName
FROM Ranked
INNER JOIN RecurRanked
ON Ranked.countryID = RecurRanked.countryID
AND Ranked.rnk = RecurRanked.rnk + 1)
SELECT countryName, MAX(stateName)
FROM RecurRanked
INNER JOIN country_master on RecurRanked.countryID = country_master.countryID
GROUP BY countryName;
你可以看到这里脱下这种技术的几种方法:https://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/
你需要的,如果要小心你的连接的城市列表将会超过8000个字符,尽管如果这是一个问题,你可能能够避免使用类似varchar(max)的东西(除了它可能不是一个很好的方法看数据)。
你应该回去,并接受正确的答案,你的过去的问题。在信用到期时给予信贷是有礼貌的,对未来类似问题的其他人有用,因为这有助于他们迅速找到正确的答案。 –