Indatabase management, anaggregate function oraggregation function is afunction where multiple values are processed together to form a singlesummary statistic.
Common aggregate functions include:
Others include:
Formally, an aggregate function takes as input aset, amultiset (bag), or alist from some input domainI and outputs an element of an output domainO.[1] The input and output domains may be the same, such as forSUM
, or may be different, such as forCOUNT
.
Aggregate functions occur commonly in numerousprogramming languages, inspreadsheets, and inrelational algebra.
Thelistagg
function, as defined in theSQL:2016 standard[2]aggregates data from multiple rows into a single concatenated string.
In theentity 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.[3]
Aggregate functions present abottleneck, because they potentially require having all input values at once. Indistributed computing, it is desirable to divide such computations into smaller pieces, and distribute the work, usuallycomputing in parallel, via adivide and conquer algorithm.
Some aggregate functions can be computed by computing the aggregate for subsets, and then aggregating these aggregates; examples includeCOUNT
,MAX
,MIN
, andSUM
. 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 includeAVERAGE
(tracking sum and count, dividing at the end) andRANGE
(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 includeDISTINCT COUNT
(Count-distinct problem),MEDIAN
, andMODE
.
Such functions are calleddecomposable aggregation functions[4] ordecomposable aggregate functions. The simplest may be referred to asself-decomposable aggregation functions, which are defined as those functionsf such that there is amerge operator such that
where is the union of multisets (seemonoid homomorphism).
For example,SUM
:
COUNT
:
MAX
:
MIN
:
Note that self-decomposable aggregation functions can be combined (formally, taking the product) by applying them separately, so for instance one can compute both theSUM
andCOUNT
at the same time, by tracking two numbers.
More generally, one can define adecomposable aggregation functionf as one that can be expressed as the composition of a final functiong and a self-decomposable aggregation functionh,. For example,AVERAGE
=SUM
/COUNT
andRANGE
=MAX
−MIN
.
In theMapReduce 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),[5] and moving decomposable aggregation before the Shuffle phase is known as an InitialReduce step,[6]
Decomposable aggregation functions are important inonline analytical processing (OLAP), as they allow aggregation queries to be computed on the pre-computed results in theOLAP cube, rather than on the base data.[7] For example, it is easy to supportCOUNT
,MAX
,MIN
, andSUM
in OLAP, since these can be computed for each cell of the OLAP cube and then summarized ("rolled up"), but it is difficult to supportMEDIAN
, as that must be computed for every view separately.
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.[8]AVG
:or
or, only if COUNT(X)=COUNT(Y)SUM(x2)
:The sum of squares of the values is important in order to calculate the Standard Deviation of groupsSTDDEV
:
For a finite population with equal probabilities at all points, we have[9][circular reference]
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.
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.
{{cite book}}
: CS1 maint: location missing publisher (link)