Aggregate Functions
Drizzle supports all standard SQL aggregate functions:Count Distinct
Count unique values:Group By
Group results by one or more columns:Having Clause
Filter grouped results:Aggregations with Joins
Combine aggregates with joined tables:Multiple Aggregates
Calculate several aggregates in one query:Aggregate functions return strings for numeric types to preserve precision. Convert to numbers if needed:
Number(result.totalRevenue)Conditional Aggregation
Aggregate with conditions:Date-based Aggregations
Group by time periods:- PostgreSQL
- MySQL
- SQLite
Window Functions
Use window functions for advanced analytics:Statistical Aggregations
Calculate statistical measures:Array Aggregation (PostgreSQL)
Aggregate values into arrays:String Aggregation
Concatenate strings:- PostgreSQL
- MySQL
- SQLite
Filtering Before Aggregation
Use WHERE to filter before grouping:Distinct Aggregations
Aggregate distinct values:Nested Aggregations with Subqueries
Aggregate over aggregated results:Type Safety
Aggregate functions are fully typed:Common Aggregation Patterns
Count by Category
Top Performers
Period Comparison
Summary Statistics
Performance Tips
1
Index Group By Columns
Ensure columns used in GROUP BY are indexed for faster aggregation
2
Filter Before Grouping
Use WHERE clauses to reduce rows before grouping
3
Limit Result Sets
Use LIMIT when you only need top N results
4
Use Covering Indexes
Create indexes that include all columns needed for the query
Next Steps
Joins
Combine aggregations with joins
Subqueries
Use aggregations in subqueries
Select Queries
Master the select query builder