|
| 1 | +--- |
| 2 | +title: Aggregation Operations |
| 3 | +description: Complete reference for grouping and aggregating data in ObjectQL |
| 4 | +--- |
| 5 | + |
| 6 | +# Aggregation Operations |
| 7 | + |
| 8 | +This document provides the complete specification for ObjectQL's Aggregation operations. |
| 9 | + |
| 10 | +## GroupBy |
| 11 | + |
| 12 | +```typescript |
| 13 | +db.aggregate(objectName: string, options: AggregateOptions) |
| 14 | + |
| 15 | +interface AggregateOptions { |
| 16 | + group_by: string[] |
| 17 | + having?: Filter[] |
| 18 | + fields?: AggregateField[] |
| 19 | + sort?: string | Sort[] |
| 20 | + limit?: number |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +### Basic GroupBy |
| 25 | + |
| 26 | +```typescript |
| 27 | +// Count by category |
| 28 | +await db.aggregate('product', { |
| 29 | + group_by: ['category'], |
| 30 | + fields: [ |
| 31 | + { field: '_id', function: 'count', alias: 'total' } |
| 32 | + ] |
| 33 | +}) |
| 34 | + |
| 35 | +// Result: [ |
| 36 | +// { category: 'Electronics', total: 45 }, |
| 37 | +// { category: 'Books', total: 123 } |
| 38 | +// ] |
| 39 | +``` |
| 40 | + |
| 41 | +### Multiple Groupings |
| 42 | + |
| 43 | +```typescript |
| 44 | +// Sales by year and quarter |
| 45 | +await db.aggregate('order', { |
| 46 | + group_by: ['YEAR(created)', 'QUARTER(created)'], |
| 47 | + fields: [ |
| 48 | + { field: 'amount', function: 'sum', alias: 'total_sales' }, |
| 49 | + { field: '_id', function: 'count', alias: 'order_count' } |
| 50 | + ], |
| 51 | + sort: 'YEAR(created) desc, QUARTER(created) desc' |
| 52 | +}) |
| 53 | +``` |
| 54 | + |
| 55 | +## Aggregate Functions |
| 56 | + |
| 57 | +```typescript |
| 58 | +// Count |
| 59 | +{ field: '_id', function: 'count' } |
| 60 | + |
| 61 | +// Sum |
| 62 | +{ field: 'amount', function: 'sum' } |
| 63 | + |
| 64 | +// Average |
| 65 | +{ field: 'price', function: 'avg' } |
| 66 | + |
| 67 | +// Min |
| 68 | +{ field: 'price', function: 'min' } |
| 69 | + |
| 70 | +// Max |
| 71 | +{ field: 'price', function: 'max' } |
| 72 | + |
| 73 | +// Distinct count |
| 74 | +{ field: 'customer_id', function: 'count_distinct' } |
| 75 | +``` |
| 76 | +
|
| 77 | +## Having Clause |
| 78 | +
|
| 79 | +Filter aggregated results: |
| 80 | +
|
| 81 | +```typescript |
| 82 | +await db.aggregate('order', { |
| 83 | + group_by: ['customer_id'], |
| 84 | + fields: [ |
| 85 | + { field: 'amount', function: 'sum', alias: 'total_spent' }, |
| 86 | + { field: '_id', function: 'count', alias: 'order_count' } |
| 87 | + ], |
| 88 | + having: [ |
| 89 | + ['total_spent', '>', 1000] // Only customers who spent > 1000 |
| 90 | + ] |
| 91 | +}) |
| 92 | +``` |
| 93 | +
|
| 94 | +## Complete Aggregation Example |
| 95 | +
|
| 96 | +```typescript |
| 97 | +// Monthly sales report with filters |
| 98 | +const monthlySales = await db.aggregate('invoice', { |
| 99 | + // Pre-aggregation filters (WHERE clause) |
| 100 | + filters: [ |
| 101 | + ['status', '=', 'paid'], |
| 102 | + ['created', '>=', '2024-01-01'] |
| 103 | + ], |
| 104 | + |
| 105 | + // Group by |
| 106 | + group_by: ['YEAR(created)', 'MONTH(created)'], |
| 107 | + |
| 108 | + // Aggregate calculations |
| 109 | + fields: [ |
| 110 | + { field: 'amount', function: 'sum', alias: 'revenue' }, |
| 111 | + { field: '_id', function: 'count', alias: 'invoice_count' }, |
| 112 | + { field: 'amount', function: 'avg', alias: 'avg_invoice' }, |
| 113 | + { field: 'customer_id', function: 'count_distinct', alias: 'unique_customers' } |
| 114 | + ], |
| 115 | + |
| 116 | + // Post-aggregation filters (HAVING clause) |
| 117 | + having: [ |
| 118 | + ['revenue', '>', 50000] // Only months with revenue > 50k |
| 119 | + ], |
| 120 | + |
| 121 | + // Sort results |
| 122 | + sort: 'YEAR(created) desc, MONTH(created) desc', |
| 123 | + |
| 124 | + // Limit results |
| 125 | + limit: 12 // Last 12 months |
| 126 | +}) |
| 127 | + |
| 128 | +// Result: [ |
| 129 | +// { |
| 130 | +// year: 2024, |
| 131 | +// month: 3, |
| 132 | +// revenue: 125000, |
| 133 | +// invoice_count: 45, |
| 134 | +// avg_invoice: 2777.78, |
| 135 | +// unique_customers: 32 |
| 136 | +// }, |
| 137 | +// ... |
| 138 | +// ] |
| 139 | +``` |
| 140 | +
|
| 141 | +## Next Steps |
| 142 | +
|
| 143 | +- Learn [Query DSL](/docs/02-objectql/protocol-spec/query-dsl) for basic queries |
| 144 | +- Master [Mutation Operations](/docs/02-objectql/protocol-spec/mutation) for data modification |
| 145 | +- Review [Schema Definition](/docs/02-objectql/protocol-spec/schema-definition) for data modeling |
0 commit comments