a package:opaleye

Given a Select producing rows of type a and an Aggregator accepting rows of type a, apply the aggregator to the select. If you simply want to count the number of rows in a query you might find the countRows function more convenient. If you want to use aggregate with SelectArrs then you should compose it with laterally:
laterally . aggregate :: Aggregator a b -> SelectArr a b -> SelectArr a b
Please note that when aggregating an empty query with no GROUP BY clause, Opaleye's behaviour differs from Postgres's behaviour. Postgres returns a single row whereas Opaleye returns zero rows. Opaleye's behaviour is consistent with the meaning of aggregating over groups of rows and Postgres's behaviour is inconsistent. When a query has zero rows it has zero groups, and thus zero rows in the result of an aggregation.
Order the values within each aggregation in Aggregator using the given ordering. This is only relevant for aggregations that depend on the order they get their elements, like arrayAgg and stringAgg. Note that this orders all aggregations with the same ordering. If you need different orderings for different aggregations, use orderAggregate.
Average of a group