All Products
Search
Document Center

AnalyticDB:ST_difference

Last Updated:Mar 28, 2026

Returns the portion of a trajectory object—within a specified time range—that falls outside a given geometry object.

Syntax

trajectory ST_difference(trajectory traj, tsrange range, geometry g);
trajectory ST_difference(trajectory traj, timestamp t1, timestamp t2, geometry g);

Two overloads are available: one accepts a tsrange for the time range, the other accepts discrete timestamp values for the start and end times.

Parameters

ParameterTypeDescription
trajtrajectoryThe source trajectory object.
rangetsrangeThe time range. Used in the first overload.
t1timestampThe start time. Used in the second overload.
t2timestampThe end time. Used in the second overload.
ggeometryThe specified geometry object.

Examples

Use discrete timestamps

The following example computes the difference between a trajectory between 13:00 and 14:00 and a line geometry. The returned trajectory contains only the trajectory segments that lie outside the LINESTRING.

SELECT ST_difference(traj, '2010-1-1 13:00:00', '2010-1-1 14:00:00', 'LINESTRING(0 0, 5 5, 9 9)'::geometry)
FROM traj_table;

What's next