Query Syntax
Holistics Query Syntax gives you more flexibility in designing your report queries. On top of the base SQL syntax supported by your database, Holistics's syntax adds a few convenient functionalities, which are:
- SQL Query Variables: Easily parameterize your queries
- SQL Query Conditions: Using if-else clauses to change your SQL conditionally
- SQL Query Templates: Setting up query templates and improve code reusability
info
The examples shown here are based on Postgres SQL or BigQuery's StandardSQL syntax.
#
SQL Query VariablesVariables can be used in Reports's queries..
#
How to useTo add a filter into your report's query, simply refer to the filter's Variable Name using double curly brackets:
The variable can be inserted in many different places in the queries. Values passed to the variable are wrapped in single quote to turn them into strings.
#
Use "noquote" tag for raw valueIn some cases, you may want to use the raw values of the filter instead of their string representation. To do so, simply a |noquote
tag after the variable name:
Example 1: In a report called "Users with Minimum X Bookings" that uses a filter "Minimum Bookings" with number values, we would want to use the raw min_bookings
number instead of a string:
Example 2: Here we want to only find products starting with the word “glass-”. The end of the product name can be inserted to the final string with a filter variable:
When you select “jar” in the Name filter, the final string will be 'glass-jar'
, while without the noquote
tag, the result would be 'glass-'jar''
, which is invalid.
#
Where to use filter variable#
In WHERE clauseIf a variable is used in a WHERE clause, double square brackets [[ ]]
is also needed around the entire condition to ensure even when there is no value passed to the variable, the final query is still valid:
For example, if you select 'Tokyo' and 'Manila' in the City dropdown filter, the query above will be translated to:
If you do not select any value for City variable, the query will be translated to:
The variable can be used to modify a string:
Because filter values are, by default, of string type, it can be concatenated with other strings.
For example, you have a Name filter and selected Bob
as value. The function above will return '%bob%'
, which is can be used in a LIKE
statement:
Let's take a look at more complicated condition clauses:
If the user selected filter values for "Sign Up From", "Sign Up To", and "Country", and leave Name, Email and Booking Number blank, this is the resulted query:
Notes
- If no value is selected for the filter, the condition will be translated to
(1 = 1)
- If the filter allows multi-select, the values will be inserted into the query as a comma-separated list. For example,
{{country}}
will become'sg','vn'
. You will need to wrap the variable in round brackets and useIN
operator like this:country IN ( {{country}} )
#
In other parts of the queryVariable and noquote
tag can be used to pass reserved keywords to the query. For example, we have a "Time Period" filter with the following values:
The keywords like month
, quarter
, year
must be passed without quote into the query, so we use noquote
tag:
If we select "Monthly" option, the query will be:
#
SQL Query Conditions#
Basic syntaxThe Query Conditions syntax allows you to use filters to conditionally change your query.
The basic syntax includes:
- An opening if tag:
{{#if ... }}
followed by a SQL or a text block - An optional else tag:
{{#else}}
followed by a SQL or a text block - The end tag:
{{#endif}}
The query can have the following forms:
In other words, you can use the syntax to conditionally run a totally new SQL, or just alter a part of your original SQL query.
#
OperatorsIn the opening {{#if ... }}
tag, generic boolean operators are supported including: and
, or
, ==
, !=
, not
.
For example:
The conditional statement can be even more complex with and
, or
and not
:
#
Use CasesConditional branching is a must-have in the most common programming languages, but unfortunately, it is not available in base SQL.
As shared above, you can now conditionally run a totally new SQL, or just alter a part of your original SQL query. This gives Holistics users even more power to control and be creative with their report logic.
#
Example 1In the following example, we use the condition syntax to add a UNION to the base query if the Chanel filter has value 'youtube':
#
Example 2Here we can conditionally modify the SELECT
statement itself:
This is equal to using three CASE ... WHEN...
statements for the three columns:
#
Query TemplatesQuery Templates are SQL snippets that can be applied and reused across different reports. For more details please refer to this page: SQL Query Templates.
The basic syntax to refer to a query template is {{ @template_name(template_var) }}
where template_var
is the name of the variable used in the template.
For example, we have the following snippet, and the syntax to refer to it in a query is:
The column can vary in different querying situation.
In a certain scenario, the full query can be: