Skip to main content
Puri lets you use SQL’s powerful aggregate functions in a type-safe manner. This document explains COUNT, SUM, AVG, MAX, MIN along with GROUP BY and HAVING.

Aggregate Function Overview

COUNT

Count records Puri.count()

SUM/AVG

Calculate sum and average Puri.sum(), Puri.avg()

MAX/MIN

Find max and min values Puri.max(), Puri.min()

GROUP BY

Aggregate by group groupBy(), having()

COUNT - Counting Records

Basic COUNT

Puri.count() generates COUNT(*). It counts all rows including NULL. Puri.count("column") counts only rows where that column is not NULL.

COUNT with GROUP BY

SUM - Sum

Calculate the sum of a numeric column.

SUM with GROUP BY

AVG - Average

Calculate the average of a numeric column.

MAX / MIN - Maximum / Minimum

Maximum

Minimum

MAX/MIN with GROUP BY

GROUP BY - Grouping

Group data by specific columns for aggregation.

Single Column GROUP BY

Multiple Column GROUP BY

GROUP BY rules: - All columns in SELECT that are not aggregate functions must be in GROUP BY - Columns not in GROUP BY cannot be SELECTed (except aggregate functions)

GROUP BY with JOIN

HAVING - Filtering Aggregate Results

HAVING filters aggregate results after GROUP BY.

Basic HAVING

WHERE vs HAVING: - WHERE: Filters before GROUP BY (on individual rows) - HAVING: Filters after GROUP BY (on aggregate results)

HAVING with Aggregate Functions

WHERE + HAVING Combined

Complex Aggregation Examples

Detailed Department Statistics

Project Progress Status

Monthly Sales Analysis

Conditional Aggregation

Conditional aggregation using CASE WHEN.

Count by Status

Range-based Aggregation

Practical Examples

Dashboard Statistics

Hire Year Statistics

Top 5 Departments

Performance Optimization

Index Usage

Only Perform Necessary Aggregations

Filter with WHERE First

Type Safety

Result types of aggregate functions are automatically inferred.

Next Steps

Raw Queries

Write complex SQL directly

Advanced Patterns

Window functions and advanced patterns

Joins

Aggregate with joins

Type Safety

Type safety of aggregate functions