J'ai un problème avec la requête CTE récursiveTSQL Un problème avec les catégories arbre
Disons que j'ai cet arbre de catégorie (tableau Catégorie)
Dans ma requête CTE, je recherche tous les enfants de la catégorie 1
: (cette requête fonctionne très bien)
with mq as
(
select c.Id as parent, c.Id as child
from dbo.Category c
where c.Id = 1
union all
select q.child, c.Id
from mq q
inner join dbo.Category c on q.child = c.IdParentCategory
)
la sortie
Alors, je veux obtenir cet ID de catégorie, wchih n'a pas d'enfant: catégories 9,10,12,14,15
with mq as
(
select c.Id as parent, c.Id as child
from dbo.Category c
where c.Id = 1
union all
select q.child, c.Id
from mq q
inner join dbo.Category c on q.child = c.IdParentCategory
where child in
(
select c1.Id
from dbo.Category c1
where not exists(select c2.Id
from dbo.Category c2
where c2.Id = c1.IdParentCategory)
)
)
mais la sortie est erroné:
pourquoi? Toutes les idées seront utiles!
si je sépare la requête du CTE, tout est OK
declare @tab table
(parent int, child int);
insert into @tab
select * from mq
delete from @tab
where child in (
select c1.parent
from @tab c1
where not exists(select c2.parent from @tab c2 where c2.parent = c1.child)
)