SapixDBSapixDB/Docs
Home
Community · SaQL

Aggregate Functions

Compute a single numeric result — total, average, extremes, or count — across all matching records in one round-trip using the aggregate query type.

Query Shape

All aggregate queries share the same structure. Send a POST to /v1/agents/:id/query with "type": "aggregate" plus the function name and the field to operate on.

SaQL — aggregate query shape
POST /v1/agents/:id/query
Authorization: Bearer spx_root_YOUR_ROOT_KEY
Content-Type: application/json

{
  "type":   "aggregate",
  "fn":     "sum | avg | min | max",
  "field":  "field_name",
  "filter": { ... }          // optional
}

Response Shape

The response contains a single aggregate object with the function name, field, computed value, and the number of records that contributed to the result.

JSON response
{
  "aggregate": {
    "fn_name":      "avg",
    "field":        "amount",
    "value":        247.50,
    "record_count": 18
  }
}
KeyTypeDescription
fn_namestringThe function that was applied (sum, avg, min, or max).
fieldstringThe record field that was aggregated.
valuenumber | nullThe computed result. null when no matching records contained a numeric value for the field.
record_countintegerNumber of records included in the computation (after any filter).

sum — Total Value

Adds up all numeric values in field across matching records. Use this for revenue totals, byte counts, event counts with weights, and similar running totals.

SaQL — total revenue
{
  "type":  "aggregate",
  "fn":    "sum",
  "field": "amount"
}
curl
curl -s -X POST http://localhost:7475/v1/agents/orders/query \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer spx_root_YOUR_ROOT_KEY" \
  -d '{"type": "aggregate", "fn": "sum", "field": "amount"}' \
  | python3 -m json.tool

avg — Average Value

Returns the arithmetic mean of field across all matching records. Typical use cases include average order value, mean response latency, and average session length.

SaQL — average order value
{
  "type":  "aggregate",
  "fn":    "avg",
  "field": "amount"
}

min — Smallest Value

Finds the smallest value of field across all matching records. Useful for finding the cheapest product in a category, the earliest login timestamp, or the lowest error rate recorded.

SaQL — minimum order value
{
  "type":  "aggregate",
  "fn":    "min",
  "field": "amount"
}

max — Largest Value

Finds the largest value of field. Common uses: largest single order, peak concurrent users, highest recorded temperature.

SaQL — largest single order
{
  "type":  "aggregate",
  "fn":    "max",
  "field": "amount"
}

Counting records

Record counting is not part of the aggregate query type. Use the dedicated count query type instead — it accepts an optional filter and returns only the record count, with no payload transfer:

SaQL — count all records
{ "type": "count" }
SaQL — count with a filter
{
  "type":   "count",
  "filter": { "field": "plan", "op": "eq", "value": "pro" }
}
JSON response
{ "count": 42 }

Filtering Aggregates

Add a filter object to scope the aggregate to a subset of records. The same filter syntax used in scan queries applies here.

SaQL — average order value for pro plan
{
  "type":   "aggregate",
  "fn":     "avg",
  "field":  "amount",
  "filter": {
    "field": "plan",
    "op":    "eq",
    "value": "pro"
  }
}
SaQL — total revenue in Q2 2026
{
  "type":   "aggregate",
  "fn":     "sum",
  "field":  "amount",
  "filter": {
    "AND": [
      { "field": "created_at", "op": "gte", "value": "2026-04-01" },
      { "field": "created_at", "op": "lt",  "value": "2026-07-01" }
    ]
  }
}

Function Reference

fnOperates onReturnsTypical use
sumnumeric fieldtotalRevenue, bytes transferred, event weights
avgnumeric fieldmeanAverage order value, mean latency
minnumeric fieldsmallestLowest price, earliest timestamp
maxnumeric fieldlargestPeak load, largest order, max score
group_by counts per bucket; aggregate computes a scalarUse aggregate for a single number across the whole strand (total revenue, mean latency). Use group_by to count how many records share each unique field value (e.g. how many users are on each plan). See Group-By Queries.