Solving Counting Problems with Aggregate Functions