跳转到主内容
跳转到主内容

timeSeriesResampleToGridWithStaleness

timeSeriesResampleToGridWithStaleness

引入版本:v25.6

该聚合函数将时间序列数据作为时间戳和值的成对数据进行处理,并根据由起始时间戳、结束时间戳和步长描述的等间隔时间网格对数据进行重新采样。对于网格上的每个点,会在给定时间窗口内选择最近的样本。

别名:timeSeriesLastToGrid

注意

该函数为实验性特性,可通过设置 allow_experimental_ts_to_grid_aggregate_function=true 来启用。

语法

timeSeriesResampleToGridWithStaleness(start_timestamp, end_timestamp, grid_step, staleness_window)(timestamp, value)

别名timeSeriesLastToGrid

参数

  • start_timestamp — 指定网格的起始时间戳。UInt32DateTime
  • end_timestamp — 指定网格的结束时间戳。UInt32DateTime
  • grid_step — 指定网格的步长(单位:秒)。UInt32
  • staleness_window — 指定最近样本的最大允许“陈旧”时间(单位:秒)。UInt32

自变量

返回值

返回重新采样到指定网格的时间序列值。返回的数组中,每个时间网格点对应一个值。如果某个网格点没有样本,则该值为 NULL。Array(Nullable(Float64))

示例

使用单个时间戳-数值对的基本用法

WITH
    -- NOTE: the gap between 140 and 190 is to show how values are filled for ts = 150, 165, 180 according to staleness window parameter
    [110, 120, 130, 140, 190, 200, 210, 220, 230]::Array(DateTime) AS timestamps,
    [1, 1, 3, 4, 5, 5, 8, 12, 13]::Array(Float32) AS values, -- array of values corresponding to timestamps above
    90 AS start_ts,       -- start of timestamp grid
    90 + 120 AS end_ts,   -- end of timestamp grid
    15 AS step_seconds,   -- step of timestamp grid
    30 AS window_seconds  -- "staleness" window
SELECT timeSeriesResampleToGridWithStaleness(start_ts, end_ts, step_seconds, window_seconds)(timestamp, value)
FROM
(
    -- This subquery converts arrays of timestamps and values into rows of `timestamp`, `value`
    SELECT
        arrayJoin(arrayZip(timestamps, values)) AS ts_and_val,
        ts_and_val.1 AS timestamp,
        ts_and_val.2 AS value
);
┌─timeSeriesResampleToGridWithStaleness(start_ts, end_ts, step_seconds, window_seconds)(timestamp, value)─┐
│ [NULL,NULL,1,3,4,4,NULL,5,8]                                                                           │
└────────────────────────────────────────────────────────────────────────────────────────────────────┘

使用数组参数

WITH
    [110, 120, 130, 140, 190, 200, 210, 220, 230]::Array(DateTime) AS timestamps,
    [1, 1, 3, 4, 5, 5, 8, 12, 13]::Array(Float32) AS values,
    90 AS start_ts,
    90 + 120 AS end_ts,
    15 AS step_seconds,
    30 AS window_seconds
SELECT timeSeriesResampleToGridWithStaleness(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values);
┌─timeSeriesResampleToGridWithStaleness(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values)─┐
│ [NULL,NULL,1,3,4,4,NULL,5,8]                                                                             │
└──────────────────────────────────────────────────────────────────────────────────────────────────────┘