Subqueries testing and grouping fix