> ## 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_first_over_time` を計算します。

# timeSeriesTimestampOfFirstToGrid

<h2 id="timeSeriesTimestampOfFirstToGrid">
  timeSeriesTimestampOfFirstToGrid
</h2>

導入バージョン: v26.10.0

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

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

* 2 つの引数 `timestamp` と `value` として渡す形式。各行が 1 つのサンプルを保持します。
* タイムスタンプの Array と値の Array の 2 つとして渡す形式。各行が 1 つの時系列全体を保持します。
* `(timestamp, value)` タプルの配列 1 つとして渡す形式。各行が 1 つの時系列全体を保持します。

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

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

**構文**

```sql theme={null}
timeSeriesTimestampOfFirstToGrid(start_timestamp, end_timestamp, grid_step, staleness_window)(timestamp, value)
timeSeriesTimestampOfFirstToGrid(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」 (データが有効とみなされる経過時間) を秒単位で指定します。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) または [`Array(Nullable(DateTime))`](/ja/reference/data-types/array) または [`Array(Nullable(DateTime64))`](/ja/reference/data-types/array)

**例**

**グリッド \[90, 105, 120, 135, 150, 165, 180, 195, 210] 上で ts\_of\_first\_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 timeSeriesTimestampOfFirstToGrid(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}
┌─timeSeriesTimestampOfFirstToGrid(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:02:10','1970-01-01 00:02:20',NULL,'1970-01-01 00:03:10','1970-01-01 00:03:10'] │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
```

**配列引数を使用した同じクエリ**

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

```response title=Response theme={null}
┌─timeSeriesTimestampOfFirstToGrid(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:02:10','1970-01-01 00:02:20',NULL,'1970-01-01 00:03:10','1970-01-01 00:03:10'] │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
```
