- Required: FROM · SHOW · SINCE
- Optional: GROUP BY · WHERE · UNTIL · TIMESERIES · SEGMENT BY · VISUALIZE
- Canonical order: FROM → SHOW → (GROUP BY / WHERE / SEGMENT BY) → SINCE → [UNTIL] → [TIMESERIES] → [SEGMENT BY] → [VISUALIZE]
Clauses
FROM
- Purpose: Selects the dataset.
- Example:
SHOW
- Purpose: Lists expressions to compute. Typically uses aggregation functions over metrics or fields.
- Examples:
GROUP BY
- Purpose: Defines the grouping keys for aggregation.
- Keys: Can be identifiers, dimensions, or product_attributes.* paths.
- TOP N Modifier: Limit results to the top N values for a dimension.
- Examples:
TOP N Modifier
The TOP N modifier limits grouped results to show only the highest-ranking values. Three variants are available:-
TOP N: Shows the top N values plus an “Other” category for the remainder.
-
ONLY TOP N: Shows only the top N values, hiding the remainder entirely.
-
TOP N OVERALL: Ranks values across the full time range rather than per-period. Useful with TIMESERIES to maintain consistent rankings.
WHERE (optional)
- Purpose: Filters rows before aggregation using boolean expressions over dimensions, fields, or attributes.
- Comparison operators:
=,!=,<,<=,>,>= - Logical operators:
AND,OR,NOT - Null handling:
IS NULL,IS NOT NULL - Precedence: Parentheses are supported for grouping conditions.
- Examples:
SINCE
- Purpose: Defines the start of the time window for the query.
- Accepted forms:
- Relative: -7d, -14d, -30d, -60d, -90d, -180d, -365d, -24h, -48h, -12m, -1y
- Keywords: today, yesterday, this_week, last_week, this_month, last_month, this_quarter, last_quarter, this_year, last_year
- Common rolling windows: past_7_days, past_14_days, past_30_days, past_60_days, past_90_days, past_180_days, past_365_days
- ISO: 2025-03-15 or 2025-03-15T10:30:00Z (timezone offsets supported)
- Examples:
UNTIL (optional)
- Purpose: Defines the end of the time window for the query. When omitted, the query runs up to the current time.
- Accepted forms: Same as SINCE (relative offsets, keywords, rolling windows, ISO timestamps).
- Examples:
The UNTIL clause is currently parsed but not yet applied during metric execution. End date filtering will be supported in a future release.
TIMESERIES (optional)
- Purpose: Groups results by time intervals with automatic backfilling for missing periods.
- Granularities:
- Standard:
day,week,month,quarter,year,hour,minute - Cyclical:
day_of_week,hour_of_day,week_of_year,month_of_year
- Standard:
- Examples:
SEGMENT BY (optional)
- Purpose: Adds an additional segmentation dimension applied to each group.
- Examples:
VISUALIZE (optional)
- Purpose: Specifies how to render the query results graphically.
- Syntax:
VISUALIZE <target> [TYPE <type>] [MAX <number>] - Target: The metric or expression to visualize (e.g.,
SUM(total_sales)or a field name). - Types:
table,bar,line,donut,horizontal_bar,stacked_bar,area,card - MAX: Limits the number of data points displayed.
- Examples:
- Identifiers: letters, digits, underscores; start with a letter or underscore.
- Paths: dot notation (for example, product_attributes.color).
- Strings: ‘value’ or “value”
- Numbers: 10, 10.5
- Booleans: TRUE, FALSE
Arithmetic Expressions
LayersQL supports arithmetic operations in both SHOW and WHERE clauses, enabling you to create calculated metrics and complex filtering conditions.Supported Operators
- Addition:
+ - Subtraction:
- - Multiplication:
* - Division:
/ - Modulo:
% - Unary minus:
-(negation)
Operator Precedence
Arithmetic expressions follow standard mathematical precedence: multiplication, division, and modulo are evaluated before addition and subtraction. Use parentheses to override the default precedence.Examples
Combine multiple aggregations:Aliasing Arithmetic Expressions
Use theAS keyword to give your calculated expressions meaningful names: