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
}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:
Before you build a segment on a query
Run it in the Query Tool
And read the number, not just the fact that it returned. See Query Tool.
Check a single user
Take one UserPoint the query returns and read their timeline. See Explore a user profile.
Related pages
Last updated
Was this helpful?