-1
With CTE as
(
Select ProductCategoryId,
Count(*) [Count],
Max(ProductName) ProductNames,
0 [Rank]
From Product
Group By ProductCategoryId
Union ALL
Select CTE.ProductCategoryId,
CTE.[Count],
ProductNames + N' , ' + ProductName,
[Rank]+1
From
CTE inner join Product
on CTE.ProductCategoryId = Product.ProductCategoryId
and CTE.ProductNames Not Like '%'+ProductName+'%'
and CTE.[Rank] < cte.[Count]
)
Select ProductCategoryId,
Max(ProductNames) ProductNames,
Max([Count]) [Count]
From CTE
Group by ProductCategoryId
order by ProductCategoryId
질문을 잊었습니다. 'ProductName'의 타입은'NVARCHAR'입니까? – StuartLC