diff --git a/docs/users-guide/11-data-model-reference.md b/docs/users-guide/11-data-model-reference.md index 9898bb2df1cb4358f4badd650abed673f0808818..90cdbd1af78d12f6a920bdae65b2ace8cc4daa8e 100644 --- a/docs/users-guide/11-data-model-reference.md +++ b/docs/users-guide/11-data-model-reference.md @@ -21,5 +21,5 @@ In addition to looking at a table's fields, you can also look at its connections --- -## That’s it! -If you still have questions, or want to share Metabase tips and tricks, head over to our [discussion board](http://discourse.metabase.com/). See you there! +## Next: powering up your SQL questions with variables +Find out [how to use variables in your native SQL queries](12-sql-parameters.md) to create powerful filter widgets and more. diff --git a/docs/users-guide/12-sql-parameters.md b/docs/users-guide/12-sql-parameters.md new file mode 100644 index 0000000000000000000000000000000000000000..8e51d50ef025f4aa3a82fc497b083c2e477e44a0 --- /dev/null +++ b/docs/users-guide/12-sql-parameters.md @@ -0,0 +1,55 @@ +## SQL Parameters +--- +Metabase has the flexible ability to allow variables in native (SQL) queries. This lets you dynamically replace values in your queries using filter widgets or through the query's URL. + + + +Options and settings for your variables will appear in the `Variables` side panel of the native query builder once you've defined a variable. So, how do you define a variable? + + + +### Defining Variables +Typing `{{variable_name}}` in your native query creates a variable called `variable_name`. Variables can be given types in the side panel, which changes their behavior. All variable types other than `field filter` will cause a filter widget to be placed on this question corresponding to the chosen variable type. When a value is selected via a filter widget, that value replaces the corresponding variable in the SQL template, wherever it appears. + +This example defines a variable called `cat`, allowing you to dynamically change the `WHERE` clause in this query: +``` +SELECT count(*) +FROM products +WHERE category = {{cat}} +``` + +#### The `Field filter` variable type +Giving a variable the `Field filter` type allows you to connect SQL cards to dashboard filter widgets. A field filter variable inserts SQL similar to that generated by the GUI query builder when adding filters on existing columns. This is important because it lets you do things like insert dynamic date range filters into your native query. When adding a field filter, you should link that variable to a specific column. Dimensions should be used inside of a `WHERE` clause. + +Example: +``` +SELECT count(*) +FROM products +WHERE {{created_at}} +``` + +### Optional Clauses +To make an optional clause in your native query, type `[[brackets around a {{variable}}]]`. If `variable` is given a value, then the entire clause is placed into the template. If not, then the entire clause is ignored. + +In this example, if no value is given to `cat` from its filter widget or URL, 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`: +``` +SELECT count(*) +FROM products +[[WHERE category = {{cat}}]] +``` + +To use multiple optional clauses you must include at least one regular `WHERE` clause followed by optional clauses, each starting with `AND`. + +Example: +``` +SELECT count(*) +FROM products +WHERE 1=1 + [[AND id = {{id}}]] + [[AND category = {{category}}]] +``` + +--- + +## That’s it! +If you still have questions, or want to share Metabase tips and tricks, head over to our [discussion board](http://discourse.metabase.com/). See you there! diff --git a/docs/users-guide/images/sql-parameters/01-variables.png b/docs/users-guide/images/sql-parameters/01-variables.png new file mode 100644 index 0000000000000000000000000000000000000000..3a68cab2635c53dd39a70907718d3f6acabfdd63 Binary files /dev/null and b/docs/users-guide/images/sql-parameters/01-variables.png differ diff --git a/docs/users-guide/images/sql-parameters/02-widget.png b/docs/users-guide/images/sql-parameters/02-widget.png new file mode 100644 index 0000000000000000000000000000000000000000..41e69281c8b893a0732c9b7cd0e0a1e77945266a Binary files /dev/null and b/docs/users-guide/images/sql-parameters/02-widget.png differ