recursion - TSQL A problem with categories tree -
i have problem recursive cte query
let's have category tree (category table)

in cte query, search children of 1 category: (that query works fine)
mq ( select c.id parent, c.id child dbo.category c c.id = 1 union select q.child, c.id mq q inner join dbo.category c on q.child = c.idparentcategory ) the output

then, want category id, wchih doesn't have child: categories 9,10,12,14,15
with mq ( select c.id parent, c.id child dbo.category c c.id = 1 union select q.child, c.id mq q inner join dbo.category c on q.child = c.idparentcategory child in ( select c1.id dbo.category c1 not exists(select c2.id dbo.category c2 c2.id = c1.idparentcategory) ) ) but output wrong:

why ? ideas helpful !
if separate query cte, ok
declare @tab table (parent int, child int); insert @tab select * mq delete @tab child in ( select c1.parent @tab c1 not exists(select c2.parent @tab c2 c2.parent = c1.child) )
with mq ( select c.id parent, c.id child dbo.category c c.id = 1 union select q.child, c.id mq q inner join dbo.category c on q.child = c.idparentcategory ) select child mq child not in (select parent mq) would seem give output want - in fact description of problem took form.
Comments
Post a Comment