Filtered Aggregates

Generic Description

Use filtered aggregates when you want an aggregate to include only rows that satisfy an explicit condition without rewriting the whole query into manual CASE expressions yourself.

Simple example:

MATCH (u:User)-[:HAS_APP_SESSION]->(s:AppSession)
RETURN count_if(s.mode = 'focused') AS focused_sessions,
       sum_if(s.duration_seconds, s.mode = 'focused') AS focused_duration

Consumer-Level Explanation

Filtered aggregates let you ask “aggregate this value, but only when a condition is true” in one expression.

The currently implemented forms are:

This is useful because a lot of product analytics and operational reporting is conditional by nature:

The point is not just convenience. It keeps the intent visible in the query instead of burying it inside application code or hand-written CASE scaffolding.

More Detailed Explanation

Nexyron lowers the filtered aggregate forms into standard aggregate semantics built from CASE WHEN ... THEN ... ELSE NULL END.

That means:

Conceptually:

This is the right tradeoff here because it keeps the implementation mathematically explicit and easy to reason about.

Advanced Example

MATCH (u:User)-[:HAS_APP_SESSION]->(s:AppSession)
TIME s.started_at BETWEEN datetime('2026-04-01T00:00:00Z') AND datetime('2026-04-30T23:59:59Z')
WITH u, s, toLower(s.mode) AS normalized_mode
RETURN u.profile.location.country AS country,
       count_if(normalized_mode = 'focused') AS focused_sessions,
       avg_if(s.duration_seconds, normalized_mode = 'focused') AS focused_avg_duration,
       max_if(s.duration_seconds, normalized_mode = 'deep') AS deepest_session,
       min_if(s.duration_seconds, s.duration_seconds >= 300) AS shortest_long_session
ORDER BY country

Real Use Cases

Real Limitations And Tradeoffs