How to get all child of a given id in SQL Server query -
i have 2 tables in sql server database:
category( itemid, parentid ) articleassignedcategories( categid, artid )
categid
foreign key of itemid
i want count of artids
, child of given itemid
(child means categories parentid
of given itemid
.)
for example; if given itemid = 1
, in table category
have (3,1),(4,1)(5,3)
all of 3, 4, 5 child of 1
can me write query?
recursive queries can done using cte
with cte(itemid, parentid) ( -- start category select itemid, parentid category itemid = <some_itemid> union -- recursively add children select c.itemid, c.parentid category c join cte on c.parentid = cte.itemid ) select count(*) articleassignedcategories join cte on cte.itemid = a.categid
Comments
Post a Comment