【发布时间】:2014-07-13 13:04:45
【问题描述】:
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
【问题讨论】:
-
你忘了问问题。是
ProductNameNVARCHAR的类型吗?
标签: sql sql-server common-table-expression