With SQL databases, we can use a [model][model] or an existing question as the basis for a new query, or as a common table expression [CTE][CTE].
With SQL databases, we can use a [model][model] or an existing question as the basis for a new query, or as a common table expression [CTE][cte].
For example, let's say we have a lot of data spread across a number of tables, but people are most interested in a subset of that data. We can perform a complicated query once to return those results, and save that question as a model, which people can refer to in their queries just like they would with any other table.
For example, let's say we have a lot of data spread across a number of tables, but people are most interested in a subset of that data. We can perform a complicated query once to return those results, and save that question as a model, which people can refer to in their queries just like they would with any other table.
...
@@ -37,14 +37,21 @@ FROM gizmo_orders
...
@@ -37,14 +37,21 @@ FROM gizmo_orders
When this query is run, the `{% raw %}{{#5-gizmo-orders-in-2019}}{% endraw %}` tag will be substituted with the SQL query of the referenced question, surrounded by parentheses. So it'll look like this under the hood:
When this query is run, the `{% raw %}{{#5-gizmo-orders-in-2019}}{% endraw %}` tag will be substituted with the SQL query of the referenced question, surrounded by parentheses. So it'll look like this under the hood:
```
```
WITH gizmo_orders AS (SELECT *
WITH
FROM orders AS o
gizmo_orders AS (
INNER JOIN products AS p
SELECT
ON o.product_id = p.id
*
WHERE p.category = 'Gizmo'
FROM
AND o.created_at BETWEEN '2019-01-01' AND '2019-12-31')
orders AS o
SELECT count(*)
INNER JOIN products AS p ON o.product_id = p.id
FROM gizmo_orders
WHERE
p.category = 'Gizmo'
AND o.created_at BETWEEN '2019-01-01' AND '2019-12-31'
@@ -18,7 +18,7 @@ Field Filter, a special type of filter, have a [slightly different syntax](#fiel
...
@@ -18,7 +18,7 @@ Field Filter, a special type of filter, have a [slightly different syntax](#fiel
This example defines a **Text** variable called `category`:
This example defines a **Text** variable called `category`:
```sql
```
{% raw %}
{% raw %}
SELECT
SELECT
count(*)
count(*)
...
@@ -31,7 +31,7 @@ WHERE
...
@@ -31,7 +31,7 @@ WHERE
Metabase will read the variable and attach a filter widget to the query, which people can use to change the value inserted into the `cat` variable with quotes. So if someone entered "Gizmo" into the filter widget, the query Metabase would run would be:
Metabase will read the variable and attach a filter widget to the query, which people can use to change the value inserted into the `cat` variable with quotes. So if someone entered "Gizmo" into the filter widget, the query Metabase would run would be:
```sql
```
SELECT
SELECT
count(*)
count(*)
FROM
FROM
...
@@ -118,7 +118,7 @@ Let's say you want to create a Field Filter that filters the `People` table by s
...
@@ -118,7 +118,7 @@ Let's say you want to create a Field Filter that filters the `People` table by s
The syntax for Field Filters differs from a Text, Number, or Date variable.
The syntax for Field Filters differs from a Text, Number, or Date variable.
```sql
```
{% raw %}
{% raw %}
SELECT
SELECT
*
*
...
@@ -196,7 +196,7 @@ The reason is that field filters generate SQL based on the mapped field; Metabas
...
@@ -196,7 +196,7 @@ The reason is that field filters generate SQL based on the mapped field; Metabas
Your main query should be aware of all the tables that your Field Filter variable is pointing to, otherwise you'll get a SQL syntax error. For example, let's say that your main query includes a field filter like this:
Your main query should be aware of all the tables that your Field Filter variable is pointing to, otherwise you'll get a SQL syntax error. For example, let's say that your main query includes a field filter like this:
```sql
```
{% raw %}
{% raw %}
SELECT
SELECT
*
*
...
@@ -209,7 +209,7 @@ WHERE
...
@@ -209,7 +209,7 @@ WHERE
Let's say the `{% raw %}{{ product_category }}{% endraw %}` variable refers to another question that uses the `Products` table. For the field filter to work, you'll need to include a join to `Products` in your main query.
Let's say the `{% raw %}{{ product_category }}{% endraw %}` variable refers to another question that uses the `Products` table. For the field filter to work, you'll need to include a join to `Products` in your main query.
```sql
```
{% raw %}
{% raw %}
SELECT
SELECT
*
*
...
@@ -248,7 +248,7 @@ In the variables sidebar, you can set a default value for your variable. This va
...
@@ -248,7 +248,7 @@ In the variables sidebar, you can set a default value for your variable. This va
You can also define default values directly in your query by enclosing comment syntax inside the end brackets of an optional parameter.
You can also define default values directly in your query by enclosing comment syntax inside the end brackets of an optional parameter.
```sql
```
WHERE column = [[ {% raw %}{{ your_parameter }}{% endraw %} --]] your_default_value
WHERE column = [[ {% raw %}{{ your_parameter }}{% endraw %} --]] your_default_value
```
```
...
@@ -256,7 +256,7 @@ The comment will "activate" whenever you pass a value to `your_parameter`.
...
@@ -256,7 +256,7 @@ The comment will "activate" whenever you pass a value to `your_parameter`.
This is useful when defining complex default values (for example, if your default value is a function like `CURRENT_DATE`). Here's a PostgreSQL example that sets the default value of a Date filter to the current date using `CURRENT_DATE`:
This is useful when defining complex default values (for example, if your default value is a function like `CURRENT_DATE`). Here's a PostgreSQL example that sets the default value of a Date filter to the current date using `CURRENT_DATE`:
```sql
```
{% raw %}
{% raw %}
SELECT
SELECT
*
*
...
@@ -286,7 +286,7 @@ To make a clause optional in your native query, type `[[brackets around a {% raw
...
@@ -286,7 +286,7 @@ To make a clause optional in your native query, type `[[brackets around a {% raw
In this example, if no value is given to `cat`, then the query will just select all the rows from the `products` table. But if `cat` does have a value, like "Widget", then the query will only grab the products with a category type of Widget:
In this example, if no value is given to `cat`, then the query will just select all the rows from the `products` table. But if `cat` does have a value, like "Widget", then the query will only grab the products with a category type of Widget:
```sql
```
{% raw %}
{% raw %}
SELECT
SELECT
count(*)
count(*)
...
@@ -297,7 +297,7 @@ FROM
...
@@ -297,7 +297,7 @@ FROM
To use multiple optional clauses, you must include at least one regular `WHERE` clause followed by optional clauses, each starting with `AND`:
To use multiple optional clauses, you must include at least one regular `WHERE` clause followed by optional clauses, each starting with `AND`: