Thursday 17 November 2011

Create columns from Parent/Child records

select a.Category, b.* from #temp a cross apply
(select ISNULL((
                LTRIM(STUFF((SELECT
                     ', ' + tTg.Category
                FROM dbo.#temp (NoLock)tTg
                WHERE tTg.ParentId = a.id FOR XML PATH('')),1,1,''))
            ),'') as Category) b
where a.ParentId = 0

No comments:

Post a Comment