Aggregate function explained

In database management, an aggregate function or aggregation function is a function where multiple values are processed together to form a single summary statistic.Common aggregate functions include:

Others include:

Formally, an aggregate function takes as input a set, a multiset (bag), or a list from some input domain and outputs an element of an output domain . The input and output domains may be the same, such as for SUM, or may be different, such as for COUNT.

Aggregate functions occur commonly in numerous programming languages, in spreadsheets, and in relational algebra.

The listagg function, as defined in the standard[1] aggregates data from multiple rows into a single concatenated string.

In the entity relationship diagram, aggregation is represented as seen in Figure 1 with a rectangle around the relationship and its entities to indicate that it is being treated as an aggregate entity.[2]

Decomposable aggregate functions

Aggregate functions present a bottleneck, because they potentially require having all input values at once. In distributed computing, it is desirable to divide such computations into smaller pieces, and distribute the work, usually computing in parallel, via a divide and conquer algorithm.

Some aggregate functions can be computed by computing the aggregate for subsets, and then aggregating these aggregates; examples include COUNT, MAX, MIN, and SUM. In other cases the aggregate can be computed by computing auxiliary numbers for subsets, aggregating these auxiliary numbers, and finally computing the overall number at the end; examples include AVERAGE (tracking sum and count, dividing at the end) and RANGE (tracking max and min, subtracting at the end). In other cases the aggregate cannot be computed without analyzing the entire set at once, though in some cases approximations can be distributed; examples include DISTINCT COUNT (Count-distinct problem), MEDIAN, and MODE.

Such functions are called decomposable aggregation functions or decomposable aggregate functions. The simplest may be referred to as self-decomposable aggregation functions, which are defined as those functions such that there is a merge operator such that

f(X\uplusY)=f(X)\diamondf(Y)

where is the union of multisets (see monoid homomorphism).

For example, SUM:

\operatorname{SUM}({x})=x

, for a singleton;

\operatorname{SUM}(X\uplusY)=\operatorname{SUM}(X)+\operatorname{SUM}(Y)

, meaning that merge is simply addition.COUNT

\operatorname{COUNT}({x})=1

,

\operatorname{COUNT}(X\uplusY)=\operatorname{COUNT}(X)+\operatorname{COUNT}(Y)

.MAX

\operatorname{MAX}({x})=x

,

\operatorname{MAX}(X\uplusY)=maxl(\operatorname{MAX}(X),\operatorname{MAX}(Y)r)

.MIN

\operatorname = x,[1]

\operatorname{MIN}(X\uplusY)=minl(\operatorname{MIN}(X),\operatorname{MIN}(Y)r)

.

Note that self-decomposable aggregation functions can be combined (formally, taking the product) by applying them separately, so for instance one can compute both the SUM and COUNT at the same time, by tracking two numbers.

More generally, one can define a decomposable aggregation function as one that can be expressed as the composition of a final function and a self-decomposable aggregation function,

f=g\circh,f(X)=g(h(X))

. For example, AVERAGE=SUM/COUNT and RANGE=MAXMIN.

In the MapReduce framework, these steps are known as InitialReduce (value on individual record/singleton set), Combine (binary merge on two aggregations), and FinalReduce (final function on auxiliary values), and moving decomposable aggregation before the Shuffle phase is known as an InitialReduce step,

Decomposable aggregation functions are important in online analytical processing (OLAP), as they allow aggregation queries to be computed on the pre-computed results in the OLAP cube, rather than on the base data. For example, it is easy to support COUNT, MAX, MIN, and SUM in OLAP, since these can be computed for each cell of the OLAP cube and then summarized ("rolled up"), but it is difficult to support MEDIAN, as that must be computed for every view separately.

Other decomposable aggregate functions

In order to calculate the average and standard deviation from aggregate data, it is necessary to have available for each group: the total of values (Σxi = SUM(x)), the number of values (N=COUNT(x)) and the total of squares of the values (Σxi2=SUM(x2)) of each groups.[3]

AVG

\operatorname(X \uplus Y) = \bigl(\operatorname(X) * \operatorname(X) + \operatorname(Y) * \operatorname(Y)\bigr) / \bigl(\operatorname(X) + \operatorname(Y)\bigr)or
\operatorname(X \uplus Y) = \bigl(\operatorname(X) + \operatorname(Y)\bigr) / \bigl(\operatorname(X) + \operatorname(Y)\bigr)or, only if COUNT(X)=COUNT(Y)
\operatorname(X \uplus Y) = \bigl(\operatorname(X) + \operatorname(Y)\bigr) / 2
SUM(x<sup>2</sup>)

The sum of squares of the values is important in order to calculate the Standard Deviation of groups
\operatorname(X^2 \uplus Y^2) = \operatorname(X^2)+\operatorname(Y^2)
STDDEV


For a finite population with equal probabilities at all points, we have[4] \operatorname(X) = s(x) = \sqrt = \sqrt= \sqrt

This means that the standard deviation is equal to the square root of the difference between the average of the squares of the values and the square of the average value.\operatorname(X \uplus Y) = \sqrt\operatorname(X \uplus Y) = \sqrt

See also

Literature

External links

Notes and References

  1. Web site: Big News in Databases: New SQL Standard, Cloud Wars, and ACIDRain (Spring 2017) . Winand . Markus . 2017-05-15 . DZone . 2017-06-10 . In December 2016, ISO released a new version of the SQL standard. It introduces new features such as row pattern matching, listagg, date and time formatting, and JSON support. . 2017-05-27 . https://web.archive.org/web/20170527080053/https://dzone.com/articles/big-news-in-databases-new-sql-standard-cloud-wars . dead .
  2. Book: Elmasri, Ramez . Fundamentals of database systems . 2016 . Sham Navathe . 978-0-13-397077-7 . Seventh . Hoboken, NJ . 133 . 913842106.
  3. Ing. Óscar Bonilla, MBA
  4. [Standard deviation#Identities and mathematical properties]