Fix db aggregate

This fixes two things:
 - the query argument of the function
 - the return type of the function. If I query for count or average, I'd expect it to return a number. This happens now.

Additionally I wrote the unit tests for the function.

The next things based on this branch will be to add a "distinct" parameter and an option to query for COUNT(*).