Tutorial: Building with CleaveDB
Named Results (AS) & Multi-Metrics
By default, DISTILL names output keys after the aggregation function used (such as "total", "avg", or "count"). With the AS keyword, you can assign descriptive names to result fields and combine multiple metrics in a single comma-separated query.
Custom output aliases with AS
Append AS <alias> directly following an aggregate expression to rename its JSON output key:
DISTILL FROM staff COUNT AS headcountOr for numeric totals:
DISTILL FROM staff TOTAL age AS payroll_ageThe database serializes the payload directly using your designated keys (e.g. {"payroll_age": 119.0}).
Combining multiple metrics in one pass
Separate multiple aggregation clauses with commas to calculate comprehensive analytical summaries simultaneously:
DISTILL FROM staff TOTAL age AS total, MIN age AS youngest, MAX age AS oldest, COUNT AS nCleaveDB computes all four metrics concurrently in a single traversal pass through the bucket, returning:
{
"total": 119.0,
"youngest": 24,
"oldest": 35,
"n": 4
}Quoting bucket names
Bucket names can be unquoted or double-quoted when adhering to strict SQL formatting styles:
DISTILL FROM "staff" COUNTAutomation with scheduled tasks (EVERY)
In production environments, DISTILL is frequently scheduled to generate recurring analytical snapshots:
EVERY 1 HOURS DO ( DISTILL FROM sales TOTAL price )