aggregate function for SQL generation