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

> 時系列データを指定したグリッドに再サンプリングする集約関数。

# timeSeriesResampleToGridWithStaleness

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

導入バージョン: v25.6.0

タイムスタンプと値のペアとして時系列データを受け取り、このデータを開始タイムスタンプ、終了タイムスタンプ、step で表される一定間隔の時間グリッドに再サンプリングする集約関数です。グリッド上の各点では、直近のサンプル (指定された time window 内のもの) が選択されます。

Alias: `timeSeriesLastToGrid`.

<Warning>
  この関数は実験的機能です。`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`](/ja/reference/data-types/int-uint) または [`DateTime`](/ja/reference/data-types/datetime)
* `end_timestamp` — グリッドの終了時刻を指定します。[`UInt32`](/ja/reference/data-types/int-uint) または [`DateTime`](/ja/reference/data-types/datetime)
* `grid_step` — グリッドの間隔を秒単位で指定します。[`UInt32`](/ja/reference/data-types/int-uint)
* `staleness_window` — 最新のサンプルについて、許容される最大の古さを秒単位で指定します。[`UInt32`](/ja/reference/data-types/int-uint)

**引数**

* `timestamp` — サンプルのタイムスタンプ。単一の値または配列を指定できます。[`UInt32`](/ja/reference/data-types/int-uint) または [`DateTime`](/ja/reference/data-types/datetime) または [`Array(UInt32)`](/ja/reference/data-types/array) または [`Array(DateTime)`](/ja/reference/data-types/array)
* `value` — タイムスタンプに対応する時系列の値。単一の値または配列を指定できます。[`Float*`](/ja/reference/data-types/float) または [`Array(Float*)`](/ja/reference/data-types/array)

**戻り値**

指定したグリッドに再サンプリングした時系列の値を返します。返される配列には、各時間グリッドポイントごとに 1 つの値が含まれます。特定のグリッドポイントに対応するサンプルがない場合、その値は NULL になります。[`Array(Nullable(Float64))`](/ja/reference/data-types/array)

**例**

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

```sql title=Query theme={null}
WITH
    -- 注意: 140と190の間のギャップは、stalenessウィンドウパラメータに従って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]                                                                           │
└────────────────────────────────────────────────────────────────────────────────────────────────────┘
```

**Array引数の使用**

```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]                                                                             │
└──────────────────────────────────────────────────────────────────────────────────────────────────────┘
```
