Explore your data warehouse using the SQL Runner. This feature allows you to preview SQL queries and subsequently create workbooks and datasets directly from SQL. 

This article will show you how to use the SQL Runner to create a new workbook or dataset. Custom SQL-based workbook data elements can also be created from within an existing workbook. Both flows use the SQL Runner. You are also able to reference your existing Sigma datasets within your SQL. 

Contents

Requirements

  • To use this feature, you must be a Creator or be assigned a custom account type with permission to write custom SQL.
  • To run SQL against a connection, you must have Can Use access your data at the connection level. Learn more
  • The SQL Runner will only appear if you have connection level access to at least one connection in your organization.

Limitations and warnings

  • When you change the parameter value, ensure that the tables use the same schema. 
  • When you use #raw parameter value, you create a security vulnerability by enabling a possible bypass of row level security in the worksheet.

Create a workbook or dataset from SQL

  1. Click the Create New button in the left side navigation panel to open the Create New / Explore Data menu. 
  2. Select Write SQL.
  3. Select your connection from the dropdown menu.
  4. The data tree on the left side of the page will allow you to explore your data.
    You may choose to click on any table to preview its data.
    Screen_Shot_2021-07-29_at_3.59.22_PM.png
  5. To write SQL, begin typing keywords into the text box in the top half of the screen. Sigma will automatically provide a list of autocomplete options to guide you. Screen_Shot_2020-08-18_at_2.28.21_PM.png

    Note: You can reference Sigma parameters in your SQL by wrapping the parameter's name in curly brackets: {{my_parameter_name}}

    If the parameter's value is being output with single quotation marks, you can remove these quotation marks by prepending the special keyword "#raw" before its name:
    {{#raw my_parameter_name}}
    If the parameter is from a "Date range" control, you can extract its start and end date:
    {{my_parameter_name}}:start and {{my_parameter_name}}:end

  6. To run your SQL query, click the blue Run button.
    Keyboard shortcuts: CTRL-Enter on a PC or CMD-Enter on a Mac.
  7. To explore your query in a workbook, click the Explore button the top right corner of the page. 
    OR
    To create a dataset, click the dropdown icon next to Explore. Then click Create Dataset.

Reference existing Sigma datasets in your SQL

You can reference your existing Sigma dataset in your SQL by using its fully qualified name as a View inside the warehouse. This will be a combination of its Location and its Dataset view name.

SELECT * FROM <location>.<dataset_warehouse_view_name>

You can find these particulars within the dataset's information panel here: Screen_Shot_2022-12-29_at_12.37.12_PM.png

Example: 

SELECT * FROM SIGMASUPPORT.WRITE_BACK.VIEW_MY_SIGMA_DATASET

Format your SQL

Use the format SQL icon button to quickly format long blocks of SQL text.

Screen_Shot_2021-07-29_at_4.01.25_PM.png

Query History

When you are using the SQL runner, you will have access to a history of the queries you've recently run against your current connection. To access this history, click the ‘History’ tab in the left hand panel.
Screen_Shot_2020-08-18_at_2.41.04_PM.png

View Multiple Tables

When writing your query, you can still use the left side menu to explore data tables. You can switch between the Table Preview and SQL results, letting you preview what columns you may want to use when joining data tables.

Examples

SELECT * FROM test.{{#raw system::CurrentUserAttributeText::schema_name}}.orders
  • The above will switch schemas based on the user if there is a schema_name user attribute. See more about this use-case here.
SELECT * FROM table
WHERE
CASE WHEN date({{Date-Range-Parameter}}:start) IS NULL
THEN DATE <= date({{Date-Range-Parameter}}:end)
WHEN date({{Date-Range-Parameter}}:end) IS NULL
THEN DATE >= date({{Date-Range-Parameter}}:start)
ELSE DATE BETWEEN date({{Date-Range-Parameter}}:start) AND date({{Date-Range-Parameter}}:end)
END
  • The above will return all rows where the [DATE] column meets the specifications of the Date-Range-Parameter. Please note that the exact syntax may change depending on how your particular warehouse interprets the parameter, that is:
    • In Snowflake, the parameter is a VARIANT, so you should use the following when extracting the start date of the date range parameter in custom SQL:
      to_timestamp({{Date-Range-Parameter}}:start)

    • In BigQuery, Trino, or Databricks, the parameter is a STRUCT, so you should use
      {{Date-Range-Parameter}}.start when extracting the start date from the parameter in custom SQL.

    • In Redshift, the parameter is a SUPER, so you should use
      select date_range.start start from (select {{Date-Range-Parameter}} date_range) to extract the parameter start date.

    • In Postgres, the parameter is a JSONB, so you should use
      ({{Date-Range-Parameter}}->>‘start’)::timestamptz when extracting the start date from the date range parameter in custom SQL.

SELECT * 
FROM EXAMPLES.BIKES.STATIONS
WHERE
CASE WHEN
LEN(ARRAY_TO_STRING(ARRAY_CONSTRUCT{{City}},',') ) = 0
THEN True
ELSE CITY in {{City}} END
  • Assuming there's a multi-select parameter called City, and that this is run on a Snowflake connection, this query will return all rows where the [CITY] column value is listed in the City parameter selection (or, when no City parameter selection is specified, it will return all rows).
SELECT * 
FROM {{#raw schema-param}}.STATIONS
  • Assuming there's a single-select/text parameter called schema-param (with a valid selection), and the STATIONS table is present in the specified schema, this will return all columns from the STATIONS table in the specified schema.

Related resources


Was this page helpful?
Yes No