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

> 一定間隔の時間グリッド上で PromQL ライクな `ts_of_last_over_time` を計算します。

# timeSeriesTimestampOfLastToGrid

<h2 id="timeSeriesTimestampOfLastToGrid">
  timeSeriesTimestampOfLastToGrid
</h2>

導入バージョン: v26.10.0

タイムスタンプと値のペアからなる時系列データを受け取り、開始タイムスタンプ、終了タイムスタンプ、ステップで定義される等間隔の時間グリッド上で、[PromQL ライクな ts\_of\_last\_over\_time](https://prometheus.io/docs/prometheus/latest/querying/functions/#aggregation_over_time) を計算する集約関数です。グリッド上の各点における結果は、指定された時間ウィンドウ内で最も新しいサンプルのタイムスタンプです。

サンプルは、次の3つの形式のいずれかで渡すことができます。

* 2 つの引数 `timestamp` と `value` として渡す方法。この場合、各行には 1 つのサンプルが含まれます。
* タイムスタンプと値の 2 つの Array として渡す方法。この場合、各行には 1 つの時系列全体が含まれます。
* `(timestamp, value)` タプルからなる 1 つの Array として渡す方法。この場合、各行には 1 つの時系列全体が含まれます。

同じタイムスタンプを持つサンプルが複数ある場合は、そのうち値が最大のサンプル1つだけが使用されます。NaN 値は他のどの値よりも優先度が低いため、NaN 値が使用されるのは、そのタイムスタンプのサンプルがすべて NaN である場合に限られます。

<Warning>
  この関数はプライベートプレビュー段階です。有効にするには、`enable_time_series_aggregate_functions=true` を設定してください。
</Warning>

**構文**

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

**パラメータ**

* `start_timestamp` — グリッドの開始時点を指定します。小数、または数値や日時を表すテキストを含む文字列も指定できます。[`UInt32`](/ja/reference/data-types/int-uint) または [`DateTime`](/ja/reference/data-types/datetime) または [`DateTime64`](/ja/reference/data-types/datetime64) または [`Float*`](/ja/reference/data-types/float) または [`Decimal*`](/ja/reference/data-types/decimal) または [`String`](/ja/reference/data-types/string)
* `end_timestamp` — グリッドの終了時点を指定します。小数、または数値や日時を表すテキストを含む文字列も指定できます。[`UInt32`](/ja/reference/data-types/int-uint) または [`DateTime`](/ja/reference/data-types/datetime) または [`DateTime64`](/ja/reference/data-types/datetime64) または [`Float*`](/ja/reference/data-types/float) または [`Decimal*`](/ja/reference/data-types/decimal) または [`String`](/ja/reference/data-types/string)
* `grid_step` — グリッドの間隔を秒単位で指定します。小数、または数値や '15s'、'1m' のような期間を含む文字列も指定できます。[`UInt32`](/ja/reference/data-types/int-uint) または [`Float*`](/ja/reference/data-types/float) または [`Decimal*`](/ja/reference/data-types/decimal) または [`String`](/ja/reference/data-types/string)
* `staleness_window` — 最新のサンプルが有効とみなされる最大経過時間 (staleness) を秒単位で指定します。小数、または数値や '15s'、'1m' のような期間を含む文字列も指定できます。[`UInt32`](/ja/reference/data-types/int-uint) または [`Float*`](/ja/reference/data-types/float) または [`Decimal*`](/ja/reference/data-types/decimal) または [`String`](/ja/reference/data-types/string)

**引数**

* `timestamp` — サンプルのタイムスタンプ。単一の値または Array を指定できます。[`UInt32`](/ja/reference/data-types/int-uint) または [`DateTime`](/ja/reference/data-types/datetime) または [`DateTime64`](/ja/reference/data-types/datetime64) または [`Array(UInt32)`](/ja/reference/data-types/array) または [`Array(DateTime)`](/ja/reference/data-types/array) または [`Array(DateTime64)`](/ja/reference/data-types/array)
* `value` — タイムスタンプに対応する時系列の値。単一の値または Array を指定できます。[`Float*`](/ja/reference/data-types/float) または [`Array(Float*)`](/ja/reference/data-types/array)
* `samples` — 時系列のサンプルを、タプル `(timestamp, value)` の配列として渡します。タプルの各要素の型は、上記のタイムスタンプおよび値の型と同じです。タイムスタンプと値を 2 つの引数に分けて渡す代わりに使用できます。[`Array(Tuple(T1, T2))`](/ja/reference/data-types/array)

**戻り値**

指定されたグリッド上における最新のサンプルのタイムスタンプを、`timestamp` と同じ型で返します。返される配列には、時間グリッドの各ポイントにつき 1 つの値が含まれます。あるグリッドポイントについて、ウィンドウ内にサンプルが存在しない場合、その値は NULL になります。 [`Array(Nullable(UInt32))`](/ja/reference/data-types/array) or [`Array(Nullable(DateTime))`](/ja/reference/data-types/array) or [`Array(Nullable(DateTime64))`](/ja/reference/data-types/array)

**例**

**グリッド \[90, 105, 120, 135, 150, 165, 180, 195, 210] 上で ts\_of\_last\_over\_time の値を計算する**

```sql title=Query theme={null}
SET enable_time_series_aggregate_functions = 1;
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 timeSeriesTimestampOfLastToGrid(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}
┌─timeSeriesTimestampOfLastToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamp, value)────────────────────────────────────────────────────┐
│ [NULL,NULL,'1970-01-01 00:02:00','1970-01-01 00:02:10','1970-01-01 00:02:20','1970-01-01 00:02:20',NULL,'1970-01-01 00:03:10','1970-01-01 00:03:30'] │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
```

**配列を引数に指定した同じクエリ**

```sql title=Query theme={null}
SET enable_time_series_aggregate_functions = 1;
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 timeSeriesTimestampOfLastToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values);
```

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