I am afraid you have an error in your SELECT as you are also picking up the Top Level categories_id that have subcategories in them ...
Is your intent to pick categories_id where there are active products "somewhere" within the branch or within the active products' active categories_id ...
Bookmarks