Back to Paradedb

Average

docs/documentation/aggregates/metrics/average.mdx

0.23.32.9 KB
Original Source

The following query computes the average value over a specific field:

<CodeGroup> ```sql SQL SELECT pdb.agg('{"avg": {"field": "rating"}}') FROM mock_items WHERE id @@@ pdb.all(); ```
python
from paradedb import Agg, All, ParadeDB

MockItem.objects.filter(
    id=ParadeDB(All())
).aggregate(agg=Agg('{"avg": {"field": "rating"}}'))
python
from sqlalchemy import select
from sqlalchemy.orm import Session
from paradedb.sqlalchemy import facets, pdb, search

stmt = (
    select(pdb.agg(facets.avg(field="rating")))
    .select_from(MockItem)
    .where(search.all(MockItem.id))
)

with Session(engine) as session:
    session.execute(stmt).all()
ruby
MockItem.search(:id)
        .match_all
        .facets_agg(agg: ParadeDB::Aggregations.avg(:rating))
</CodeGroup>
ini
              agg
-------------------------------
 {"value": 3.8536585365853657}
(1 row)

See the Tantivy documentation for all available options.

SQL Average Syntax

SQL's AVERAGE syntax is supported in beta. To enable it, first run

sql
SET paradedb.enable_aggregate_custom_scan TO on;

With this feature enabled, the following query is equivalent to the above and is executed in the same way.

<CodeGroup> ```sql SQL SELECT AVG(rating) FROM mock_items WHERE id @@@ pdb.all(); ```
python
from django.db.models import Avg
from paradedb import All, ParadeDB

MockItem.objects.filter(
    id=ParadeDB(All())
).aggregate(avg_rating=Avg('rating'))
python
from sqlalchemy import func, select
from sqlalchemy.orm import Session
from paradedb.sqlalchemy import search

stmt = (
    select(func.avg(MockItem.rating))
    .select_from(MockItem)
    .where(search.all(MockItem.id))
)

with Session(engine) as session:
    session.execute(stmt).all()
ruby
MockItem.search(:id).match_all.average(:rating)
</CodeGroup>

By default, AVG ignores null values. Use COALESCE to include them in the final average:

<CodeGroup> ```sql SQL SELECT AVG(COALESCE(rating, 0)) FROM mock_items WHERE id @@@ pdb.all(); ```
python
from django.db.models import Avg, Value
from django.db.models.functions import Coalesce
from paradedb import All, ParadeDB

MockItem.objects.filter(
    id=ParadeDB(All())
).aggregate(avg_rating=Avg(Coalesce('rating', Value(0))))
python
from sqlalchemy import func, select
from sqlalchemy.orm import Session
from paradedb.sqlalchemy import search

stmt = (
    select(func.avg(func.coalesce(MockItem.rating, 0)))
    .select_from(MockItem)
    .where(search.all(MockItem.id))
)

with Session(engine) as session:
    session.execute(stmt).all()
ruby
rating = MockItem.arel_table[:rating]
coalesced_rating = Arel::Nodes::NamedFunction.new("COALESCE", [rating, Arel::Nodes.build_quoted(0)])

MockItem.search(:id).match_all.average(coalesced_rating)
</CodeGroup>