Aggregate Functions
Compute sum, avg, min, max, count over a field in a single query.
What you'll learn
- ✓type: aggregate with fn and field
- ✓fn values: sum, avg, min, max, count
- ✓Response: {aggregate: {fn, field, value, count}}
- ✓Aggregate with filter — scoped aggregation
- ✓Use case: revenue totals, average order value, min/max pricing
Write 20 orders (amount 1–1000). Compute sum, avg, min, max. Filter to amount > 500 and recompute avg.
Interactive Walkthrough
What you'll learn
Compute sum, average, min, max, and count over a field in one query.
Aggregate query shape
{
"type": "aggregate",
"fn": "sum | avg | min | max | count",
"field": "field_name",
"filter": { ... }
}Examples
`bash
# Total revenue
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
# Average order value 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": "avg", "field": "amount"}' \ | python3 -m json.tool
# Largest single order 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": "max", "field": "amount"}' \ | python3 -m json.tool
# Average order value for pro users only
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": "avg",
"field": "amount",
"filter": {"field": "plan", "op": "eq", "value": "pro"}
}' | python3 -m json.tool
`
Response:
`json
{ "aggregate": { "fn": "avg", "field": "amount", "value": 247.50, "count": 18 } }
`
Challenge
Write 20 orders with random amounts (1–1000). Compute sum, avg, min, max. Then filter to orders > 500 and recompute avg.
---