這個問題本質上是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)的東西(除了它可能不是一個很好的方法看數據)。
你應該回去,並接受正確的答案,你的過去的問題。在信用到期時給予信貸是有禮貌的,對未來類似問題的其他人有用,因爲這有助於他們迅速找到正確的答案。 –