Sample a temporal value with respect to an interval
tsample({tnumber,tgeompoint},duration interval,torigin timestamptz='2000-01-03',
interp='discrete') →{tnumber,tgeompoint}
If the origin is not specified, it is set by default to Monday, January 3, 2000. The given interval must be strictly greater than zero.
SELECT tsample(tint '{1@2001-01-01,5@2001-01-05}', '3 days', '2001-01-01'); -- {1@2001-01-01} SELECT tsample(tfloat '[1@2001-01-01,5@2001-01-05]', '1 day', '2001-01-01'); -- {1@2001-01-01, 2@2001-01-02, 3@2001-01-03, 4@2001-01-04, 5@2001-01-05} SELECT tsample(tfloat '[1@2001-01-01,5@2001-01-05]', '3 days', '2001-01-01'); -- {1@2001-01-01, 4@2001-01-04} SELECT tsample(tfloat '[1@2001-01-01,5@2001-01-05]', '3 days', '2001-01-01', 'linear'); -- [1@2001-01-01, 4@2001-01-04]
SELECT asText(tsample(tgeompoint '[Point(1 1)@2001-01-01, Point(5 5)@2001-01-05]', '2 days', '2001-01-01')); -- {POINT(1 1)@2001-01-01, POINT(3 3)@2001-01-03, POINT(5 5)@2001-01-05} SELECT asText(tsample(tgeompoint '{[Point(1 1)@2001-01-01, Point(5 5)@2001-01-05], [Point(1 1)@2001-01-06, Point(5 5)@2001-01-08]}', '3 days', '2001-01-01')); -- {POINT(1 1)@2001-01-01, POINT(4 4)@2001-01-04, POINT(3 3)@2001-01-07} SELECT asText(tsample(tgeompoint '{[Point(1 1)@2001-01-01, Point(5 5)@2001-01-05], [Point(1 1)@2001-01-06, Point(5 5)@2001-01-08]}', '3 days', '2001-01-01', 'step')); -- Interp=Step;[POINT(1 1)@2001-01-01, POINT(4 4)@2001-01-04, POINT(3 3)@2001-01-07]
Figure 9.2, “Sampling of temporal floats with discrete, step, and linear interpolation.” illustrates the sampling of temporal floats with various interpolations. As shown in the figure, the sampling operation is best suited for temporal values with continuous interpolation.
Reduce the temporal precision of a temporal value with respect to an interval computing the time-weighted average/centroid in each time bin
tprecision({tnumber,tgeompoint},duration interval,torigin timestamptz='2000-01-03')
→ {tnumber,tpoint}
If the origin is not specified, it is set by default to Monday, January 3, 2000. The given interval must be strictly greater than zero.
SELECT tprecision(tint '[1@2001-01-01,5@2001-01-05,1@2001-01-09]','1 day', '2001-01-01'); -- Interp=Step;[1@2001-01-01, 5@2001-01-05, 1@2001-01-09] SELECT tprecision(tfloat '[1@2001-01-01,5@2001-01-05,1@2001-01-09)','1 day', '2001-01-01'); -- [1.5@2001-01-01, 4.5@2001-01-04, 4.5@2001-01-05, 1.5@2001-01-08] SELECT tprecision(tfloat '[1@2001-01-01,5@2001-01-05,1@2001-01-09]','1 day', '2001-01-01'); -- [1.5@2001-01-01, 4.5@2001-01-04, 4.5@2001-01-05, 1.5@2001-01-08, 1@2001-01-09] SELECT tprecision(tfloat '[1@2001-01-01,5@2001-01-05,1@2001-01-09)','2 days', '2001-01-01'); -- [2@2001-01-01, 4@2001-01-03, 4@2001-01-05, 2@2001-01-07]
SELECT asText(tprecision(tgeompoint '[Point(1 1)@2001-01-01, Point(5 5)@2001-01-05, Point(1 1)@2001-01-09)', '1 day', '2001-01-01')); /* [POINT(1.5 1.5)@2001-01-01, POINT(4.5 4.5)@2001-01-04, POINT(4.5 4.5)@2001-01-05, POINT(1.5 1.5)@2001-01-08] */ SELECT asText(tprecision(tgeompoint '[Point(1 1)@2001-01-01, Point(5 5)@2001-01-05, Point(1 1)@2001-01-09)', '2 days', '2001-01-01')); /* [POINT(2 2)@2001-01-01, POINT(4 4)@2001-01-03, POINT(4 4)@2001-01-05, POINT(2 2)@2001-01-07] */ SELECT asText(tprecision(tgeompoint '[Point(1 1)@2001-01-01, Point(5 5)@2001-01-05, Point(1 1)@2001-01-09)', '4 days', '2001-01-01')); -- [POINT(3 3)@2001-01-01, POINT(3 3)@2001-01-05]
Changing the precision of a temporal value is akin to changing its temporal granularity, for example, from timestamps to hours or days, although the precision can be set to an arbitrary interval, such as 2 hours and 15 minutes. Figure 9.3, “Changing the precision of temporal floats with discrete, step, and linear interpolation.” illustrates a change of temporal precision for temporal floats with various interpolations.