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

# timeSeriesCountToGrid

<h2 id="timeSeriesCountToGrid">
  timeSeriesCountToGrid
</h2>

導入バージョン: v26.9.0

タイムスタンプと値のペアからなる時系列データを受け取り、開始タイムスタンプ、終了タイムスタンプ、step で定義される一定間隔の時間グリッド上で、[PromQL ライクな count\_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 つの Array として渡す形式。この場合、各行が 1 つの時系列全体に対応します。

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

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

**構文**

```sql theme={null}
timeSeriesCountToGrid(start_timestamp, end_timestamp, grid_step, staleness)(timestamp, value)
timeSeriesCountToGrid(start_timestamp, end_timestamp, grid_step, staleness)(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` — 対象とするサンプルの最大 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)` のタプルのArrayとして渡す時系列のサンプル。タプルの各要素の型は、上記のタイムスタンプおよび値の型です。タイムスタンプと値を 2 つの引数に分けて渡す代わりに使用できます。[`Array(Tuple(T1, T2))`](/ja/reference/data-types/array)

**戻り値**

指定したグリッド上のサンプル数を返します。返されるArrayには、時間グリッドの各点につき 1 つの値が含まれます。あるグリッド点のウィンドウ内にサンプルが存在しない場合、その値は NULL になります。[`Array(Nullable(UInt64))`](/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 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
    45 AS window_seconds  -- "staleness" window
SELECT timeSeriesCountToGrid(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}
┌─timeSeriesCountToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamp, value)─┐
│ [NULL,NULL,2,3,4,2,1,1,3]                                                               │
└─────────────────────────────────────────────────────────────────────────────────────────┘
```

**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,
    45 AS window_seconds
SELECT timeSeriesCountToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values);
```

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