Я знаю, что пост довольно старый, но вот как я это сделал.
В моей таблице есть несколько корневых категорий, поэтому я использую @RootId, чтобы указать, какая из них мне нужна. Для корневой категории ParentID установлено значение NULL.
Если в вашей таблице категорий нет столбца CatLevel - cte Tree создаст его для вас. Если вы собираетесь часто использовать запрос, лучше добавить его в таблицу (код UPDATE комментируется в конце).
Если вы хотите иметь динамическое число столбцов (не фиксированное до 5, как в моем случае), сделайте динамический SQL-запрос для него.
DECLARE @RootId INT, @SearchId INT
SET @RootId = 0
SET @SearchId = 0
--SELECT it.Id, it.ParentId, it.Name, it.RootId FROM ItemTree it
--WHERE (@RootId = 0 OR it.RootId = @RootId) AND (@SearchId = 0 OR it.Id = @SearchId)
;WITH Tree AS --gets category levels
(
SELECT 1 as CatLevel, it.Id, it.ParentId, it.Name, it.RootId
FROM ItemTree it WHERE it.ParentId IS NULL AND (@RootId = 0 OR it.RootId = @RootId) AND (@SearchId = 0 OR it.Id = @SearchId)
UNION ALL
SELECT t.CatLevel + 1 as CatLevel, it.Id, it.ParentId, it.Name, it.RootId
FROM ItemTree it
INNER JOIN Tree t ON it.ParentId = t.Id
WHERE (@RootId = 0 OR it.RootId = @RootId) AND (@SearchId = 0 OR it.Id = @SearchId)
----if CatLevel already exists in the table, just use it instead for better performance
--SELECT it.CatLevel, it.Id, it.ParentId, it.Name FROM ItemTree it
--WHERE (@RootId = 0 OR it.RootId = @RootId) AND (@SearchId = 0 OR it.Id = @SearchId)
)
, LeafIds AS --use it to show only leaf categories
(
SELECT DISTINCT Id FROM ItemTree it WHERE (@RootId = 0 OR it.RootId = @RootId)
EXCEPT
SELECT DISTINCT ParentId FROM ItemTree it WHERE (@RootId = 0 OR it.RootId = @RootId)
)
, LeafTree AS --prepare data for pivots
(
SELECT t.CatLevel, t.Id, t.ParentId, t.Name, Id AS LeafId
FROM Tree t --WHERE Id IN (SELECT Id FROM LeafIds)
UNION ALL
SELECT t.CatLevel, t.Id, t.ParentId, t.Name, lt.LeafId
FROM Tree t
INNER JOIN LeafTree lt ON lt.ParentId = t.Id
)
, TreeFinal AS --make pivots for Id and Name over same leafIds
(
SELECT t.*, pt1.[1] AS ID_1, pt1.[2] AS ID_2, pt1.[3] AS ID_3, pt1.[4] AS ID_4, pt1.[5] AS ID_5
, pt2.[1] AS Name_1, pt2.[2] AS Name_2, pt2.[3] AS Name_3, pt2.[4] AS Name_4, pt2.[5] AS Name_5
, ISNULL(pt2.[1], '') + ISNULL(' - ' + pt2.[2], '') + ISNULL(' - ' + pt2.[3], '') + ISNULL(' - ' + pt2.[4], '') + ISNULL(' - ' + pt2.[5], '') AS Path
FROM Tree t
JOIN (
SELECT LeafId, CatLevel, Id
FROM LeafTree
) source
PIVOT ( MAX(Id) FOR CatLevel IN ([1], [2], [3], [4], [5]) ) AS pt1 ON pt1.LeafId = t.Id
JOIN (
SELECT LeafId, CatLevel, Name
FROM LeafTree
) source
PIVOT ( MAX(Name) FOR CatLevel IN ([1], [2], [3], [4], [5]) ) AS pt2 ON pt2.LeafId = t.Id
)
--Use following to update CatLevel in your table, if it was not there before
--UPDATE it SET CatLevel = Tree.CatLevel FROM Tree JOIN dbo.ItemTree it ON it.Id = Tree.Id
SELECT * FROM TreeFinal ORDER BY Path
Вот скриншот запросов до и после