For the complete documentation index, see llms.txt. This page is also available as Markdown.

OTQL examples

Worked OTQL queries, from how resolution works to the ones that look right and are not.

Your goal

See the language applied to real questions - and recognise the shapes that produce a wrong answer without failing.

How a query is resolved

OTQL syntax is close to SQL, but the execution is not: it works on object trees, not columns. Resolution is a two-phase process.

For these examples, take this runtime schema:

type UserPoint {
    id: ID!
    activities: [UserActivity!]!
    profiles: [UserProfile!]!
}

type UserActivity {
    id: ID!
    events: [UserEvent!]!
}

type UserEvent {
    id: ID!
    name: String
}

type UserProfile {
    id: ID!
    age: String
}
1

Narrow the queried object

The WHERE clause is applied to the object named in FROM, and to any sub-object's fields.

2

Return the desired objects and fields

Only what is listed in the SELECT clause is returned.

The query above means "get all event names, for users with at least one confirmed transaction" - so the result contains all the events of those users, not only the transactions:

Changing the scope of a query

The same underlying question, asked from different starting points:

Pick the FROM that matches what you want counted or returned, then use WHERE and JOIN for the conditions.

Scoring: monetary and frequency conditions

For these examples, take this schema:

The question: count UserPoints who bought more than €1,000 of IT products. The answer depends on what "more than €1,000" means.

The threshold is applied inside one order: the sum of IT product prices in a single order must exceed 1,000.

The threshold moves up to activity_events, so the sum is across all orders. The explicit form makes the score roles visible:

The date condition sits on the order, next to the products - not on the outer scoring.

Two conditions on the same collection: one summing prices, one counting items. Note that the second has no @ScoreField, so its score is a count.

@ScoreMax with @ScoreField is how you express "at least one item worth more than X" - as opposed to @ScoreSum, which would test the total.

Date queries

Users celebrating their birthday today, aged 18 to 28 - one rounded anchor per rolling year:

Users celebrating their birthday in the next 7 days, aged 18 to 28 - a range per year:

Filtering what is returned

@filter restricts the objects returned by SELECT, which is exactly what WHERE does not do.

All URLs of type newsarticle and category actu browsed yesterday, per user:

The result contains empty arrays for users who matched the WHERE but have no event matching the filter. That is expected: the WHERE selected the user, the @filter selected the events.

Remember that @filter in SELECT is not compatible with aggregation operations - see OTQL documentation.

Before you build a segment on a query

1

Run it in the Query Tool

And read the number, not just the fact that it returned. See Query Tool.

2

Sanity-check the magnitude

Compare against a count you already know - total UserPoints, or the same query with one condition removed. A scoring query that means something other than you intended usually returns a suspiciously round or suspiciously large number.

3

Check a single user

Take one UserPoint the query returns and read their timeline. See Explore a user profile.

4

Remember what changes in a segment

LIMIT is ignored during segment calculation, and now is evaluated when the calculation starts.

Last updated

Was this helpful?