米田です。
文字列を各レコードから集計的に結合する方法
文字列版 SUM() 関数はありませんので、これまでできないと思い込んでいました。
SET NOCOUNT ON
DECLARE @Str0 NVARCHAR(3000)
SET @Str0 = ''
SELECT @Str0 = @Str0 + CategoryName
FROM Northwind.dbo.Categories
ORDER BY CategoryID
SELECT @Str0
こんなコードで CategoryName を結合できます。
BeveragesCondimentsConfections
といった結果が得られます。
(逆順は ORDER BY CategoryID DESC )
注意点としては、
http://support.microsoft.com/default.aspx?scid=kb;EN-US;287515
287515 - PRB: Execution Plan and Results of Aggregate Concatenation Queries Depend Upon Expression Location