# Count children of FINDER_ROOTs without using PR_SUBFOLDERS
#
SELECT COUNT(*)
FROM properties AS p
INNER JOIN hierarchy AS hp ON p.hierarchyid=hp.id AND p.tag=12289
INNER JOIN hierarchy AS hc ON hp.id=hc.parent
WHERE p.val_string="FINDER_ROOT";

# Uses PR_SUBFOLDERS hack to count
# (It's an integer rather than a boolean in the KC DB)
#
SELECT SUM(p2.val_ulong)
FROM properties AS p1
INNER JOIN properties AS p2
ON p1.hierarchyid=p2.hierarchyid AND p1.tag=12289 AND p2.tag=13834
WHERE p1.val_string="FINDER_ROOT";

# Show top 25 of stores with the most number of immediate search folders
#
SELECT hp.id, COUNT(*) AS c
FROM hierarchy AS hp
INNER JOIN hierarchy AS hs
ON hp.id=hs.parent AND hp.type=3 AND hs.type=3 AND hp.flags=1 and hs.flags=2
GROUP BY hp.id ORDER BY c DESC LIMIT 25;
