Improving typecasting for aggregate db functions

Some SQL aggregate functions (like [MAX()](http://dev.mysql.com/doc/refman/5.5/en/group-by-functions.html#function_max)) may take not numbers but strings. So typecasting returned value to int or float is not always correct.