<!-- source: https://secondlayer.tools/docs/api-reference#aggregate-subgraph-rows -->

## Aggregate a table's rows

`GET /v1/subgraphs/{name}/{table}/aggregate`

Scalar aggregates over the rows that match the filters: count, distinct counts, and sum, min and max of numeric columns. With no aggregate param it returns `{ "count": n }`. Up to 32 aggregate columns per request. Takes the same column filters and `_search` as the row list.

### Path parameters

| Name | Type | Description |
| --- | --- | --- |
| `name` | string, required | Subgraph name, as deployed (the `name` in its definition). |
| `table` | string, required | Table name, one of the keys of the definition's `schema`. `GET /v1/subgraphs/{name}` lists them. |

### Query parameters

| Name | Type | Description |
| --- | --- | --- |
| `_count` | string | Include `count`. Any value but `false` turns it on. `count` is also returned when no other aggregate is asked for. |
| `_countDistinct` | string | Comma-separated columns to count distinct values of. Any column. |
| `_sum` | string | Comma-separated columns to sum. Numeric only: `uint` or `int` columns, or `_block_height`; others are 400 `NON_NUMERIC_COLUMN`. |
| `_min` | string | Comma-separated numeric columns to take the minimum of. |
| `_max` | string | Comma-separated numeric columns to take the maximum of. |
| `_search` | string | Case-insensitive contains match across every column marked `search: true` in the definition, ORed. 400 on a table with no searchable column. |
| `filters` | object | Column filters, one query param each, ANDed. Any table column or system column (`_id`, `_block_height`, `_tx_id`, `_created_at`) works. `col=value` is equality; `col.op=value` applies an operator, one of `gte`, `lte`, `gt`, `lt`, `neq`, `like`, `in`, `notIn`. `like` is a case-insensitive contains match. `in` and `notIn` take a comma-separated list. An unknown column is 400 `INVALID_COLUMN`; an unknown operator, or the misplaced form `col=op.value`, is 400 `VALIDATION_ERROR`. Bare `limit`, `offset`, `sort`, `order`, `fields` and `search` are 400: control params take a leading underscore. |

### Returns

One `SubgraphAggregates` object.

### Responses

- `200` The aggregates
- `400` A column or filter was refused: `INVALID_COLUMN`, `NON_NUMERIC_COLUMN`, `TOO_MANY_AGGREGATES`, or `VALIDATION_ERROR`
- `401` This bind requires the instance token, and it was missing or wrong
- `404` No readable subgraph by that name (`SUBGRAPH_NOT_FOUND`), or it has no such table (`TABLE_NOT_FOUND`)
- `429` Hosted only: rate limited; retry after `Retry-After` seconds. Self-hosted reads are not rate limited

### Example

```bash
curl -G https://api.secondlayer.tools/v1/subgraphs/{name}/{table}/aggregate \
  -H "Authorization: Bearer $SECONDLAYER_API_KEY" \
  -d filters=[object Object]
```

```json
{
  "count": 3,
  "countDistinct": {
    "sender": 3
  },
  "sum": {
    "amount": "42000"
  },
  "max": {
    "amount": "14000"
  }
}
```
