Aggregate functions
The most common aggregate functions (in an arbitrary order) are AVG()
, COUNT()
, MAX()
, MIN()
, and SUM()
, including their DISTINCT
variants. I'm pretty sure that you are very familiar with these aggregates and you've used them in many of your queries. For instance, here are two SELECT
statements that compute the popular harmonic and geometric means for sales grouped by fiscal year. Here, we use the jOOQ sum()
and avg()
functions:
// Harmonic mean: n / SUM(1/xi), i=1…n
ctx.select(SALE.FISCAL_YEAR, count().divide(
sum(inline(1d).divide(SALE.SALE_))).as("harmonic_mean"))
.from(SALE).groupBy(SALE.FISCAL_YEAR).fetch();
And here, we compute the geometric mean:
// Geometric mean: EXP(AVG(LN(n))) ctx.select(SALE.FISCAL_YEAR, exp(avg(ln(SALE.SALE_))) .as("geometric_mean")) .from(SALE).groupBy...