fix query to aggregate by Threads (GROUP BY)