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

> 在等间隔时间网格上计算类似 PromQL 的 `ts_of_min_over_time`。

# timeSeriesTimestampOfMinToGrid

<h2 id="timeSeriesTimestampOfMinToGrid">
  timeSeriesTimestampOfMinToGrid
</h2>

引入版本：v26.9.0

聚合函数，接收由时间戳和值对构成的时间序列数据，并在由起始时间戳、结束时间戳和步长定义的等间隔时间网格上，基于这些数据计算[类似 PromQL 的 ts\_of\_min\_over\_time](https://prometheus.io/docs/prometheus/latest/querying/functions/#aggregation_over_time)。对于网格上的每个点，结果为指定时间窗口内取最小值的最新样本的时间戳 (以秒为单位) 。

样本可以通过以下三种形式之一传入：

* 以 `timestamp` 和 `value` 两个参数传入，每行包含一个样本；
* 以时间戳 array 和值 array 两个 array 传入，每行包含一整条时间序列；
* 以由 `(timestamp, value)` Tuple 组成的单个 array 传入，每行包含一整条时间序列。

值为 NaN 的样本会被忽略，除非窗口内的所有样本均为 NaN。

如果多个样本的时间戳相同，则只使用其中值最大的那个样本。NaN 值比任何其他值都小，因此只有当该时间戳上的所有样本均为 NaN 时，才会使用 NaN 值。

<Note>
  此函数目前处于私有预览阶段，可通过设置 `enable_time_series_aggregate_functions=true` 启用。
</Note>

**语法**

```sql theme={null}
timeSeriesTimestampOfMinToGrid(start_timestamp, end_timestamp, grid_step, staleness)(timestamp, value)
timeSeriesTimestampOfMinToGrid(start_timestamp, end_timestamp, grid_step, staleness)(samples)
```

**参数**

* `start_timestamp` — 指定网格的起点。也可以是小数，或包含数字或日期时间文本的字符串。[`UInt32`](/zh/reference/data-types/int-uint) 或 [`DateTime`](/zh/reference/data-types/datetime) 或 [`DateTime64`](/zh/reference/data-types/datetime64) 或 [`Float*`](/zh/reference/data-types/float) 或 [`Decimal*`](/zh/reference/data-types/decimal) 或 [`String`](/zh/reference/data-types/string)
* `end_timestamp` — 指定网格的终点。也可以是小数，或包含数字或日期时间文本的字符串。[`UInt32`](/zh/reference/data-types/int-uint) 或 [`DateTime`](/zh/reference/data-types/datetime) 或 [`DateTime64`](/zh/reference/data-types/datetime64) 或 [`Float*`](/zh/reference/data-types/float) 或 [`Decimal*`](/zh/reference/data-types/decimal) 或 [`String`](/zh/reference/data-types/string)
* `grid_step` — 指定网格的步长，单位为秒。也可以是小数，或包含数字或时长 (如 '15s' 或 '1m') 的字符串。[`UInt32`](/zh/reference/data-types/int-uint) 或 [`Float*`](/zh/reference/data-types/float) 或 [`Decimal*`](/zh/reference/data-types/decimal) 或 [`String`](/zh/reference/data-types/string)
* `staleness` — 指定参与计算的样本的最大“陈旧时间”，单位为秒。陈旧窗口为左开右闭区间。也可以是小数，或包含数字或时长 (如 '15s' 或 '1m') 的字符串。[`UInt32`](/zh/reference/data-types/int-uint) 或 [`Float*`](/zh/reference/data-types/float) 或 [`Decimal*`](/zh/reference/data-types/decimal) 或 [`String`](/zh/reference/data-types/string)

**参数**

* `timestamp` — 样本的时间戳。可以是单个值，也可以是 array。[`UInt32`](/zh/reference/data-types/int-uint) 或 [`DateTime`](/zh/reference/data-types/datetime) 或 [`DateTime64`](/zh/reference/data-types/datetime64) 或 [`Array(UInt32)`](/zh/reference/data-types/array) 或 [`Array(DateTime)`](/zh/reference/data-types/array) 或 [`Array(DateTime64)`](/zh/reference/data-types/array)
* `value` — 与时间戳对应的时间序列值。可以是单个值，也可以是 array。[`Float*`](/zh/reference/data-types/float) 或 [`Array(Float*)`](/zh/reference/data-types/array)
* `samples` — 时间序列样本，以 `(timestamp, value)` 元组 array 的形式传入，其中元组元素的类型分别与上文所列的时间戳和值类型相同。可用于替代将时间戳和值作为两个独立参数传入的方式。[`Array(Tuple(T1, T2))`](/zh/reference/data-types/array)

**返回值**

指定网格上的 `ts_of_min_over_time` 值：即各最小值对应的时间戳，类型与 `timestamp` 相同。返回的 array 中，每个时间网格点对应一个值。如果某个网格点的窗口内没有样本，则对应值为 NULL。[`Array(Nullable(UInt32))`](/zh/reference/data-types/array) 或 [`Array(Nullable(DateTime))`](/zh/reference/data-types/array) 或 [`Array(Nullable(DateTime64))`](/zh/reference/data-types/array)

**示例**

**在网格 \[90, 105, 120, 135, 150, 165, 180, 195, 210, 225] 上计算 ts\_of\_min\_over\_time 值**

```sql title=Query theme={null}
SET enable_time_series_aggregate_functions = 1;
WITH
    -- NOTE: the gap between 130 and 190 is to show how values are filled for ts = 180 according to window parameter
    [110, 120, 130, 190, 200, 210, 220, 230]::Array(DateTime) AS timestamps,
    [1, 3, 2, 6, 6, 4, 2, 0]::Array(Float32) AS values, -- array of values corresponding to timestamps above
    90 AS start_ts,       -- start of timestamp grid
    90 + 135 AS end_ts,   -- end of timestamp grid
    15 AS step_seconds,   -- step of timestamp grid
    45 AS window_seconds  -- "staleness" window
SELECT timeSeriesTimestampOfMinToGrid(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
);
```

```response title=Response theme={null}
┌─timeSeriesTimestampOfMinToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamp, value)───────────────────────────────────────────────────────────────────────────┐
│ [NULL,NULL,'1970-01-01 00:01:50','1970-01-01 00:01:50','1970-01-01 00:01:50','1970-01-01 00:02:10',NULL,'1970-01-01 00:03:10','1970-01-01 00:03:30','1970-01-01 00:03:40'] │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
```

**使用 array 参数的同一查询**

```sql title=Query theme={null}
SET enable_time_series_aggregate_functions = 1;
WITH
    [110, 120, 130, 190, 200, 210, 220, 230]::Array(DateTime) AS timestamps,
    [1, 3, 2, 6, 6, 4, 2, 0]::Array(Float32) AS values,
    90 AS start_ts,
    90 + 135 AS end_ts,
    15 AS step_seconds,
    45 AS window_seconds
SELECT timeSeriesTimestampOfMinToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values);
```

```response title=Response theme={null}
┌─timeSeriesTimestampOfMinToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values)─────────────────────────────────────────────────────────────────────────┐
│ [NULL,NULL,'1970-01-01 00:01:50','1970-01-01 00:01:50','1970-01-01 00:01:50','1970-01-01 00:02:10',NULL,'1970-01-01 00:03:10','1970-01-01 00:03:30','1970-01-01 00:03:40'] │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
```
