moved orphaned ID queries to CTE