> ## Documentation Index
> Fetch the complete documentation index at: https://docs.oasm.io/llms.txt
> Use this file to discover all available pages before exploring further.

# 集計とグループ化

> Attack Surface Query を数値に変える:groupBy、count、countDistinct、sum、min、max、avg、条件付き集計、having、時系列、タイムゾーン

リストは *どれか* に答え、集計は *いくつか* に答えます。クエリに `aggregate` を追加すると、レコードごとに 1 行ではなく、グループごとに 1 行が返されます。

*各ポートを開いているホストはいくつあるか?*

```json theme={null}
{
  "from": "services",
  "groupBy": ["port"],
  "aggregate": { "hosts": { "fn": "countDistinct", "field": "host.id" } },
  "orderBy": [{ "field": "hosts", "dir": "desc" }],
  "limit": 20
}
```

```json theme={null}
{ "rows": [ { "port": 443, "hosts": 118 }, { "port": 80, "hosts": 69 }, { "port": 8080, "hosts": 33 } ] }
```

## aggregate

自分で選んだ名前から `{ "fn", "field"?, "where"? }` へのマップです。1 つのクエリにつき最大 **8 個** まで指定できます。

| `fn` | フィールド | 結果 |
| - | - | - |
| `count` | なし | グループ内の行数 |
| `count` | フィールド | そのフィールドに値がある行数 |
| `countDistinct` | フィールド | 一意の値の数(`host.id` はサービスではなくホストを数える) |
| `sum` | 数値 | 合計 |
| `avg` | 数値 | 平均 |
| `min`、`max` | 数値、時刻、テキスト、列挙型 | 最小値 / 最大値 |

集計の名前をグループキーと同じにすることはできません。

### 条件付き集計

集計に独自の `where` を与えると、一致する行だけを数えます。これにより、1 つのクエリで、すべての列が同じ行に対して計算された行列全体を返せます。

*ホストごとの未対応の検出結果を、深刻度ごとの列付きで:*

```json theme={null}
{
  "from": "vulnerabilities",
  "groupBy": ["host.value"],
  "where": { "logic": "and", "items": [{ "field": "isOpen", "op": "eq", "value": "true" }] },
  "aggregate": {
    "open": { "fn": "count" },
    "critical": { "fn": "count", "where": { "logic": "and", "items": [{ "field": "severity", "op": "eq", "value": "critical" }] } },
    "high": { "fn": "count", "where": { "logic": "and", "items": [{ "field": "severity", "op": "eq", "value": "high" }] } },
    "maxCvss": { "fn": "max", "field": "cvssScore" }
  },
  "orderBy": [{ "field": "critical", "dir": "desc" }]
}
```

```json theme={null}
{ "rows": [
  { "host": { "value": "api.example.com" }, "open": 7, "critical": 2, "high": 3, "maxCvss": 9.8 },
  { "host": { "value": "shop.example.com" }, "open": 4, "critical": 1, "high": 0, "maxCvss": 9.1 }
] }
```

最初にクエリの `where` が適用され、次に各集計の独自のフィルターが適用されます。集計のフィルターには、ネストしたグループやリレーションのテストを含め、[`where`](https://docs.oasm.io/ja/attack-surface-query/filtering) が受け付けるものをすべて指定できます。

## groupBy

グループキーは最大 3 つです。キーは [リソースとフィールド](https://docs.oasm.io/ja/attack-surface-query/resources.md) で *groupable* とされているフィールドで、リソース自体または関連する行のものを指定できます。

```json theme={null}
{ "from": "hosts", "groupBy": ["target.value", "kind"], "aggregate": { "n": { "fn": "count" } } }
```

`groupBy` がない場合、クエリは合計の 1 行を返します。

```json theme={null}
{ "from": "vulnerabilities",
  "aggregate": { "open": { "fn": "count" }, "maxCvss": { "fn": "max", "field": "cvssScore" } },
  "where": { "logic": "and", "items": [{ "field": "isOpen", "op": "eq", "value": "true" }] } }
```

## having:グループを絞り込む

`where` は数える前にレコードを絞り込み、`having` は数えた後にグループを絞り込みます。フィールドにはグループキーと集計の名前を使い、`where` と同じ形(グループ、`not`)を取ります。

*緊急または高の検出結果が 1 件以上あるホスト:*

```json theme={null}
{
  "from": "vulnerabilities",
  "groupBy": ["host.value"],
  "aggregate": {
    "critical": { "fn": "count", "where": { "logic": "and", "items": [{ "field": "severity", "op": "eq", "value": "critical" }] } },
    "high": { "fn": "count", "where": { "logic": "and", "items": [{ "field": "severity", "op": "eq", "value": "high" }] } }
  },
  "having": { "logic": "or", "items": [
    { "field": "critical", "op": "gt", "value": "0" },
    { "field": "high", "op": "gt", "value": "0" }
  ] }
}
```

`having` は、すべてのキーで `eq`、`neq`、`in`、`notIn` を、数値と時刻で `gt`、`gte`、`lt`、`lte` を受け付けます。件数は数値として比較され(`"value": "0"`)、グループキーはそれ自身の型(列挙値、ポートなど)で比較されます。

## 時系列

### バケット

時刻フィールドは、バケット(`hour`、`day`、`week`、`month`)で期間ごとにグループ化します。

```json theme={null}
{
  "window": { "since": "now-30d" },
  "queries": {
    "hostsPerDay": {
      "from": "hosts",
      "groupBy": [{ "field": "firstSeen", "bucket": "day" }],
      "aggregate": { "n": { "fn": "count" } },
      "limit": 31
    }
  }
}
```

バケットのない時刻フィールドはグループキーにできません。

### 空のバケットを埋める

デフォルトでは、何もない日には行がないため、グラフに穴が空きます。`"fill": true` を追加すると、クエリは **ウィンドウのすべてのバケット** を返し、空のものには `0` が入ります。

```json theme={null}
{
  "window": { "since": "now-14d" },
  "queries": {
    "newPortsPerDay": {
      "from": "services",
      "groupBy": [{ "field": "firstSeen", "bucket": "day" }],
      "aggregate": { "n": { "fn": "count" } },
      "fill": true
    }
  }
}
```

```json theme={null}
{ "rows": [
  { "firstSeen": "2026-11-06T00:00:00.000Z", "n": 0 },
  { "firstSeen": "2026-11-07T00:00:00.000Z", "n": 12 },
  { "firstSeen": "2026-11-08T00:00:00.000Z", "n": 0 },
  "…"
] }
```

* `fill` には `window`(系列がそれをカバーします)と、バケット付きの時刻であるグループキーがちょうど 1 つ必要です。
* 行は時系列順に返され、`limit` のデフォルトはバケットの数です。
* 空のバケットでは、`count`、`countDistinct`、`sum` は `0`、`min`、`max`、`avg` は `null` になります。
* 系列は行数の上限に収まる必要があります。14 日間を日単位なら問題ありませんが、1 年間を時間単位では収まりません。より大きなバケットか、より短いウィンドウを使ってください。

### タイムゾーン

バッチで指定しない限り、バケットは **UTC** で区切られます。ハノイの 1 日は UTC の 1 日より 7 時間早く始まるため、そこにいるチーム向けの日次グラフでは、そのタイムゾーンを指定すべきです。

```json theme={null}
{
  "window": { "since": "now-14d" },
  "options": { "timezone": "Asia/Ho_Chi_Minh" },
  "queries": { "newPortsPerDay": { "...": "..." } }
}
```

`timezone` には任意の IANA 名(`Europe/Paris`、`America/New_York`、`UTC` など)を指定でき、バッチのすべてのバケットに適用されます。レスポンスの時刻は ISO 8601 の時点のままです。ハノイの 11 月 7 日のバケットは `2026-11-06T17:00:00.000Z` として返されます。

## 並び順とサイズ

* `orderBy` にはグループキーまたは集計の名前を指定します:`{ "field": "hosts", "dir": "desc" }`。デフォルトは最初の集計の降順(埋められた系列では時系列順)で、同順位はグループキーに従います。
* `limit` はグループの数を制限します(デフォルト 50)。
* 集計クエリでは `select`、`include`、`cursor`、`withTotal` は使えません。

## ロールアップ:大規模なワークスペース向けの即時集計

`rollups` リソースは、OASM が各サービスの最新の HTTP レスポンスに対して 15 分ごとに再計算する件数を保持します。ワークスペースの規模にかかわらず、即座に読み取れます。

| `metric` | `key` | `value` |
| - | - | - |
| `port` | ポート番号 | そのポートを開いているサービス数 |
| `technology` | テクノロジー名 | それを実行しているサービス数 |
| `webserver` | `Server` ヘッダー | それを返しているサービス数 |
| `statusCode` | HTTP ステータスコード | それを返しているサービス数 |
| `tlsVersion` | TLS バージョン | それをネゴシエートしているサービス数 |

```json theme={null}
{ "from": "rollups", "select": ["key", "value", "computedAt"],
  "where": { "logic": "and", "items": [{ "field": "metric", "op": "eq", "value": "technology" }] },
  "limit": 20 }
```

ロールアップはワークスペース全体のもので、プロジェクト内では読み取れず、ライブデータより最大 15 分遅れます。正確な件数や絞り込んだ件数が必要な場合は、代わりにライブのリソースを集計してください。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.