Building a Dynamic Query to Aggregate Results