Tutorial: Building with CleaveDB
Group & Aggregate Stages
In addition to standard filtering and projection, CleaveDB allows powerful group-and-aggregate stages inside a pipeline. The output of an aggregation stage is itself a stream of structured records that can be further sorted, limited, or transformed by downstream THEN clauses.
Multi-metric group aggregation
Group documents by an attribute and compute multiple statistical metrics in one pass:
PIPE FROM emp
THEN GROUP BY dept
TALLY AS n,
MIN OF age AS youngest,
MAX OF age AS oldest,
AVERAGE OF age AS avgThis groups all employee records by department and emits structured summary objects containing the count, minimum age, maximum age, and average age per department.
Sorting by computed aggregate aliases
Because stages feed forward into the next stage, you can sort and limit by the computed aliases produced during aggregation:
PIPE FROM orders
THEN WHERE status = "completed"
THEN GROUP BY region TOTAL OF revenue AS region_total
THEN ARRANGED BY region_total GOING DOWN
THEN LIMIT 5Notice how ARRANGED BY region_total GOING DOWN directly targets the alias created by the preceding TOTAL OF revenue AS region_total stage, and THEN LIMIT 5 yields the top 5 highest-revenue regions.
Supported aggregate functions in PIPE
| Function | Syntax pattern | Description |
|---|---|---|
| Count / Tally | TALLY AS n | Counts documents matching the partition |
| Total / Sum | TOTAL OF price AS total_price | Sums numeric values |
| Average | AVERAGE OF score AS avg_score | Calculates arithmetic mean |
| Minimum / Maximum | MIN OF age AS min_a, MAX OF age AS max_a | Calculates extreme values |
| Spread | SPREAD OF latency AS latency_range | Calculates the difference between max and min |
