RESAMPLE
constructsupportedQueries & DMLpreviewReturns a data set that includes both input rows and generated rows for missing data points, given a user-defined time-based granularity.
Syntax
supportedpartialnot supportednot scored
FROM <object_reference> [ [ AS ] <alias_name> ]
RESAMPLE(
USING <time_series_column>
INCREMENT BY <time_series_constant>
[ PARTITION BY <partition_column> [ , ... ] ]
[ METADATA_COLUMNS
{ IS_GENERATED() | BUCKET_START() } [ [ AS ] <alias_name> ] [ , ... ] ]
)
Parameters
| Name | Required | Description |
|---|---|---|
FROM object_reference | required | Specifies the name of a table or another object reference that contains the input data set, such as a subquery. For more information about object references, see FROM. |
USING time_series_column | required | Specifies the column that contains time-based values in the time series. The column must be a date and time data type or a numeric data type. For example, UNIX timestamp values might be stored in NUMBER(38,0) columns, where 1743447600 is equivalent to 2025-3-31 12:00:00. |
INCREMENT BY time_series_constant | required | Specifies an INTERVAL constant or a numeric constant, depending on the data type of the USING column. This constant represents the width of each time interval. The slices are aligned relative to midnight on January 1, 1970 (1970-01-01 00:00:00). The TIME_SLICE function uses the same alignment; for more information, see the TIME_SLICE usage notes. When the USING parameter specifies a date or time column, the INCREMENT BY expression must be an INTERVAL constant. When the USING parameter specifies a numeric column, the INCREMENT BY expression must also be numeric. The starting point for a generated time series is based on the minimum time of the time_series_constant. If this constant is a numeric constant, it must be positive (greater than 0). |
[ AS ] alias_name | optional | Specifies an alternative name for the object reference. The alias can be used in any other subclause within the FROM clause. Alias names must follow the rules for Object identifiers. |
PARTITION BY partition_column | optional | Partitions the result set on one or more input columns and generates new rows within each partition. |
METADATA_COLUMNS {function} [ [ AS ] {alias_name} ] | optional | Adds one or more metadata columns to the resampled result set. To add the columns, call one or both of the following functions: If you specify both metadata columns, separate them with a comma. Generated columns can have aliases. Alias names must follow the rules for Object identifiers.
|
Usage notes
- An INTERVAL constant in the INCREMENT BY clause has the following requirements: The constant must be equal to or greater than one second. Smaller units (millisecond, microsecond, nanosecond) aren’t supported.
- When the USING column is a DATE data type, you can’t specify a unit in the interval that is more granular than day (hour, minute, second). For example, the constants 'INTERVAL 1 day, 2 hours' and 'INTERVAL 25 hours' aren’t allowed.
- To avoid ambiguity, certain date parts can’t be mixed. The supported date parts fall into three discrete groups: year, quarter, month
- week
- day, hour, minute, second
- For example, the following intervals, which cross these group boundaries, aren’t allowed:
- INTERVAL '1 week, 3 days'
- INTERVAL '2 weeks, 12 hours'
- INTERVAL '3 months, 1 week'
- With respect to joins, the RESAMPLE construct works in a similar way to the SAMPLE / TABLESAMPLE construct. Resampling applies to only one table, not all preceding tables or the entire expression prior to the RESAMPLE clause. To resample the result of a join, use a subquery for the join, then resample the resulting table. See Sampling with joins.
- The RESAMPLE clause is evaluated before WHERE clause conditions are applied. If you want to resample a filtered data set, filter it first (for example, by creating a new table that you can resample or by using a subquery that is computed first inside the main RESAMPLE query). The following query resamples the whole table, then discards everything but the rows for Atlanta and Boston. Copy code SELECT * FROM heavy_weather RESAMPLE( USING start_time INCREMENT BY INTERVAL '1 day') WHERE city IN('Atlanta','Boston') ORDER BY start_time, city, county; A potential rewrite with a subquery would be: Copy code SELECT * FROM (SELECT * FROM heavy_weather WHERE city IN('Atlanta','Boston')) RESAMPLE( USING start_time INCREMENT BY INTERVAL '1 day') ORDER BY start_time, city, county;
- When resampled rows are generated, they contain NULL values for columns that aren’t partitioned. If you use a WHERE clause to filter columns that aren’t partitioned, your filter might be applied to generated rows in unexpected ways. For example, because the following query does not have a PARTITION BY clause, it does not work as expected. In the generated rows, city and county are NULL. The WHERE clause then filters out all generated rows; only the original rows that match the filter criteria are preserved. Copy code SELECT * FROM march_temps RESAMPLE( USING observed INCREMENT BY INTERVAL '5 minutes') WHERE city = 'Big Bear City' AND county = 'San Bernardino' ORDER BY observed; To solve the problem, the following query partitions by city and county. The RESAMPLE clause generates rows for combinations of city and county that exist in the source data. The WHERE clause condition preserves the appropriate generated rows because city and county have non-NULL values in those rows. Copy code SELECT * FROM march_temps RESAMPLE( USING observed INCREMENT BY INTERVAL '5 minutes' PARTITION BY city, county) WHERE city = 'Big Bear City' AND county = 'San Bernardino' ORDER BY observed; In comparison,