Возможно, у кого-то есть лучший способ выполнить последнюю часть (избавиться от промежуточных строк), но это поможет вам без жесткого кодирования любых соединений:
declare @Categories table
(
categoryID int,
categoryParentID int,
categoryDesc nvarchar(100)
)
insert into @Categories values
(1,NULL,'Categories'),
(4,NULL,'Instrument'),
(55,NULL,'Genre'),
(65,NULL,'Geographical Place '),
(8,1,'CLASSICAL'),
(47,1,'SOLO INSTRUMENTS'),
(10694,1,'STYLES'),
(4521,4,'Piano'),
(1137,8,'SOLO INSTRUMENTS'),
(1140,8,'WALTZES'),
(841,47,'PIANO'),
(93328,55,'CLASSICAL'),
(93331,55,'BLUES'),
(93334,55,'CLUB / ELECTRONICA'),
(93339,55, 'CONTEMPORARY FOLK'),
(93344,55,'CHILDREN'),
(94892,65,'EUROPE'),
(4180,10694,'CLASSICAL - SOLO PIANO'),
(94893,94892,'Western & Southern Europe'),
(94900,94893,'France')
;with CategoryCTE (categoryID, categoryString) as
(
select categoryID, cast(categoryDesc as nvarchar(max)) as categoryString
from @Categories
where categoryParentID is null
union all
select rsCat.categoryID, cast(CategoryCTE.categoryString + N'>>' + rsCat.categoryDesc as nvarchar(max))
from @Categories rsCat
inner join CategoryCTE on rsCat.categoryParentID = CategoryCTE.categoryID
)
select categoryString
from CategoryCTE o
where not exists
(
--eliminate intermediate strings
select i.categoryString
from CategoryCTE i
where LEN(i.categoryString) != LEN(o.categoryString)
and CHARINDEX(o.categoryString, i.categoryString) > 0
)
Рабочий пример.