> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-8a08bda2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> 按指定网格对时间序列数据进行重采样的聚合函数。

# timeSeriesResampleToGridWithStaleness

<div id="timeSeriesResampleToGridWithStaleness">
  ## timeSeriesResampleToGridWithStaleness
</div>

引入版本：v25.6.0

该聚合函数接收由时间戳和值构成的时间序列数据，并将其重采样到由起始时间戳、结束时间戳和步长定义的规则时间网格上。对于网格上的每个点，会选择最近的一个样本 (位于指定时间窗口内) 。

别名：`timeSeriesLastToGrid`。

<Warning>
  此函数为 Experimental，请通过设置 `allow_experimental_ts_to_grid_aggregate_function=true` 启用。
</Warning>

**语法**

```sql theme={null}
timeSeriesResampleToGridWithStaleness(start_timestamp, end_timestamp, grid_step, staleness_window)(timestamp, value)
```

**别名**: `timeSeriesLastToGrid`

**参数**

* `start_timestamp` — 指定网格的起始时间。[`UInt32`](/zh/reference/data-types/int-uint) 或 [`DateTime`](/zh/reference/data-types/datetime)
* `end_timestamp` — 指定网格的结束时间。[`UInt32`](/zh/reference/data-types/int-uint) 或 [`DateTime`](/zh/reference/data-types/datetime)
* `grid_step` — 指定网格步长，单位为秒。[`UInt32`](/zh/reference/data-types/int-uint)
* `staleness_window` — 指定最近样本允许的最大陈旧时长，单位为秒。[`UInt32`](/zh/reference/data-types/int-uint)

**实参**

* `timestamp` — 样本的时间戳。可以是单个值，也可以是数组。[`UInt32`](/zh/reference/data-types/int-uint) 或 [`DateTime`](/zh/reference/data-types/datetime) 或 [`Array(UInt32)`](/zh/reference/data-types/array) 或 [`Array(DateTime)`](/zh/reference/data-types/array)
* `value` — 与时间戳对应的时间序列值。可以是单个值，也可以是数组。[`Float*`](/zh/reference/data-types/float) 或 [`Array(Float*)`](/zh/reference/data-types/array)

**返回值**

返回重采样到指定网格的时间序列值。返回的数组在每个时间网格点上都包含一个值。如果某个网格点没有样本，则该值为 NULL。[`Array(Nullable(Float64))`](/zh/reference/data-types/array)

**示例**

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

```sql title=Query theme={null}
WITH
    -- 注意：140 和 190 之间的间隔用于展示如何根据 staleness window 参数为 ts = 150、165、180 填充值
    [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  -- "staleness" 窗口
SELECT timeSeriesResampleToGridWithStaleness(start_ts, end_ts, step_seconds, window_seconds)(timestamp, value)
FROM
(
    -- 此子查询将时间戳数组和值数组转换为 `timestamp`、`value` 行
    SELECT
        arrayJoin(arrayZip(timestamps, values)) AS ts_and_val,
        ts_and_val.1 AS timestamp,
        ts_and_val.2 AS value
);
```

```response title=Response theme={null}
┌─timeSeriesResampleToGridWithStaleness(start_ts, end_ts, step_seconds, window_seconds)(timestamp, value)─┐
│ [NULL,NULL,1,3,4,4,NULL,5,8]                                                                           │
└────────────────────────────────────────────────────────────────────────────────────────────────────┘
```

**使用数组参数**

```sql title=Query theme={null}
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);
```

```response title=Response theme={null}
┌─timeSeriesResampleToGridWithStaleness(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values)─┐
│ [NULL,NULL,1,3,4,4,NULL,5,8]                                                                             │
└──────────────────────────────────────────────────────────────────────────────────────────────────────┘
```
