> ## 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 ライクな `last_over_time` を計算します。

# timeSeriesLastToGrid

<h2 id="timeSeriesLastToGrid">
  timeSeriesLastToGrid
</h2>

導入バージョン: v25.6.0

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

別名: `timeSeriesResampleToGridWithStaleness`。

サンプルは次の 3 つの形式のいずれかで渡せます。

* 2 つの引数 `timestamp` と `value` として渡す形式。各行に 1 つのサンプルが格納されます。
* タイムスタンプと値の 2 つの Array として渡す形式。各行に 1 つの時系列全体が格納されます。
* `(timestamp, value)` タプルからなる単一の Array として渡す形式。各行に 1 つの時系列全体が格納されます。

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

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

**構文**

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

**別名**: `timeSeriesResampleToGridWithStaleness`

**パラメータ**

* `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)` のArrayとして渡す時系列のサンプル。タプルの各要素は、上記のタイムスタンプおよび値の型を持ちます。タイムスタンプと値を別々の2つの引数として渡す代わりに使用できます。[`Array(Tuple(T1, T2))`](/ja/reference/data-types/array)

**戻り値**

指定したグリッドに再サンプリングされた時系列の値を、`value` と同じ型で返します。返されるArrayには、時間グリッドの各点につき1つの値が含まれます。グリッド点に対応するサンプルが存在しない場合、その値は NULL になります。[`Array(Nullable(Float*))`](/ja/reference/data-types/array)

**例**

**個々のタイムスタンプと値のペアを使用した基本的な使い方**

```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 timeSeriesLastToGrid(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}
┌─timeSeriesLastToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamp, value)─┐
│ [NULL,NULL,1,3,4,4,NULL,5,8]                                                           │
└────────────────────────────────────────────────────────────────────────────────────────┘
```

**Array引数を使用する**

```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 timeSeriesLastToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values);
```

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