Skip to content

feat Aggregation functions (sum, avg, min, max) #11

@Ox54

Description

@Ox54

Summary

Extend aggregation support beyond .count(). Implement .sum(), .avg(), .min(), and .max() (or equivalent) so users can run aggregations in the database instead of loading all rows and computing in Python.

Current state

Status: Partially implemented.

  • Implemented: .count()
  • Not implemented: .sum(), .avg(), .min(), .max()

Documentation references:

  • docs/coming-soon.md (Aggregation Functions)
  • docs/guide/queries.md (lines 166-172)

Current workaround: Use .count() where possible; otherwise load records and compute aggregations in Python.

Proposed API (examples)

# Works today
total_users = await User.count()

# Does NOT work yet
total_sales = await Order.sum(Order.amount)
avg_price = await Product.avg(Product.price)
min_price = await Product.min(Product.price)
max_price = await Product.max(Product.price)
  • Aggregations should accept a field (or expression) and work with the existing query builder (e.g. with filters: Order.filter(Order.status == "paid").sum(Order.amount)).

Acceptance criteria

  • .sum(field) implemented and returns a single scalar value.
  • .avg(field) implemented and returns a single scalar value.
  • .min(field) implemented and returns a single scalar value.
  • .max(field) implemented and returns a single scalar value.
  • Aggregations work with filters (e.g. .filter(...).sum(...)).
  • Docs updated: remove from Coming Soon and document in the queries guide alongside .count().

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions