The temporal aggregate functions generalize the traditional aggregate functions. Their semantics is that they compute the value of the function at every instant in the union of the temporal extents of the values to aggregate. In contrast, recall that all other functions manipulating temporal types compute the value of the function at every instant in the intersection of the temporal extents of the arguments.
The temporal aggregate functions are the following ones:
For all temporal types, the function tcount
generalize the traditional function count
. The temporal count can be used to compute at each point in time the number of available objects (for example, number of cars in an area).
For all temporal types, function extent
returns a bounding box that encloses a set of temporal values. Depending on the base type, the result of this function can be a period
, a tbox
or an stbox
.
For the temporal Boolean type, the functions tand
and tor
generalize the traditional functions and
and or
.
For temporal numeric types, there are two types of temporal aggregate functions. The functions tmin
, tmax
, tsum
, and tavg
generalize the traditional functions min
, max
, sum
, and avg
. Furthermore, the functions wmin
, wmax
, wcount
, wsum
, and wavg
are window (or cumulative) versions of the traditional functions that, given a time interval w, compute the value of the function at an instant t by considering the values during the interval [t-w, t]. All window aggregate functions are available for temporal integers, while for temporal floats only window minimum and maximum are meaningful.
For the temporal text type, the functions tmin
y tmax
generalize the traditional functions min
and max
.
Finally, for temporal point types, the function tcentroid
generalizes the function ST_Centroid
provided by PostGIS. For example, given set of objects that move together (that is, a convoy or a flock) the temporal centroid will produce a temporal point that represents at each instant the geometric center (or the center of mass) of all the moving objects.
In the examples that follow, we suppose the tables Department
and Trip
contain the two tuples introduced in Section 3.1.
Temporal count
tcount(ttype): {tint_instset,tint_seqset}
SELECT tcount(NoEmps) FROM Department; -- "{[1@2012-01-01, 2@2012-02-01, 1@2012-08-01, 1@2012-10-01)}"
Bounding box extent
extent(temp): {period,tbox,stbox}
SELECT extent(noEmps) FROM Department; -- "TBOX((4,2012-01-01 00:00:00+01),(12,2012-10-01 00:00:00+02))" SELECT extent(Trip) FROM Trips; -- "STBOX T((0,0,2012-01-01 08:00:00+01),(3,3,2012-01-01 08:20:00+01))"
Temporal and
tand(tbool): tbool
SELECT tand(NoEmps #> 6) FROM Department; -- "{[t@2012-01-01, f@2012-04-01, f@2012-10-01)}"
Temporal or
tor(tbool): tbool
SELECT tor(NoEmps #> 6) FROM Department; -- "{[t@2012-01-01, f@2012-08-01, f@2012-10-01)}"
Temporal minimum
tmin(ttype): {ttype_instset,ttype_seqset}
SELECT tmin(NoEmps) FROM Department; -- "{[10@2012-01-01, 4@2012-02-01, 6@2012-06-01, 6@2012-10-01)}"
Temporal maximum
tmax(ttype): {ttype_instset,ttype_seqset}
SELECT tmax(NoEmps) FROM Department; -- "{[10@2012-01-01, 12@2012-04-01, 6@2012-08-01, 6@2012-10-01)}"
Temporal sum
tsum(tnumber): {tnumberinstset,tnumberseqset}
SELECT tsum(NoEmps) FROM Department; -- "{[10@2012-01-01, 14@2012-02-01, 16@2012-04-01, 18@2012-06-01, 6@2012-08-01, 6@2012-10-01)}"
Temporal average
tavg(tnumber): {tfloat_instset,tfloat_seqset}
SELECT tavg(NoEmps) FROM Department; -- "{[10@2012-01-01, 10@2012-02-01), [7@2012-02-01, 7@2012-04-01), [8@2012-04-01, 8@2012-06-01), [9@2012-06-01, 9@2012-08-01), [6@2012-08-01, 6@2012-10-01)"
Window minimum
wmin(tnumber,interval): {tnumberinstset,tnumberseqset}
SELECT wmin(NoEmps, interval '2 days') FROM Department; -- "{[10@2012-01-01, 4@2012-04-01, 6@2012-06-03, 6@2012-10-03)}"
Window maximum
wmax(tnumber,interval): {tnumberinstset,tnumberseqset}
SELECT wmax(NoEmps, interval '2 days') FROM Department; -- "{[10@2012-01-01, 12@2012-04-01, 6@2012-08-03, 6@2012-10-03)}"
Window count
wcount(tnumber,interval): {tint_instset,tint_seqset}
SELECT wcount(NoEmps, interval '2 days') FROM Department; -- "{[1@2012-01-01, 2@2012-02-01, 3@2012-04-01, 2@2012-04-03, 3@2012-06-01, 2@2012-06-03, 1@2012-08-03, 1@2012-10-03)}"
Window sum
wsum(tint,interval): {tint_instset,tint_seqset}
SELECT wsum(NoEmps, interval '2 days') FROM Department; -- "{[10@2012-01-01, 14@2012-02-01, 26@2012-04-01, 16@2012-04-03, 22@2012-06-01, 18@2012-06-03, 6@2012-08-03, 6@2012-10-03)}"
Window average
wavg(tint,interval): {tfloat_instset,tfloat_seqset}
SELECT wavg(NoEmps, interval '2 days') FROM Department; -- "{[10@2012-01-01, 10@2012-02-01), [7@2012-02-01, 7@2012-04-01), [8.66666666666667@2012-04-01, 8.66666666666667@2012-04-03), [8@2012-04-03, 8@2012-06-01), [7.33333333333333@2012-06-01, 7.33333333333333@2012-06-03), [9@2012-06-03, 9@2012-08-03), [6@2012-08-03, 6@2012-10-03)}"
Temporal centroid
tcentroid(tgeompoint): tgeompoint
SELECT tcentroid(Trip) FROM Trips; -- "{[POINT(0 0)@2012-01-01 08:00:00+00, POINT(1 0)@2012-01-01 08:05:00+00), [POINT(0.5 0)@2012-01-01 08:05:00+00, POINT(1.5 0.5)@2012-01-01 08:10:00+00, POINT(2 1.5)@2012-01-01 08:15:00+00), [POINT(2 2)@2012-01-01 08:15:00+00, POINT(3 3)@2012-01-01 08:20:00+00)}"