#956 - generate and attach subquery to base query