SQL Snippets

Snippets are fragments of SQL code that can be reused in your analysis across your charts. Snippets allow better isolation, standardization and reuse of business logic.

Creating SQL Snippets

You'll want to rename your snippet to better reflect what it does:

Using Snippets in SQL

Let's assume you've created a snippet name paid_users, with the following SQL:

is_paid = True

You can now easily use this snippet in your query in the following way:

select id, count(customer_id) 
from customers
where [paid_users];

that gets rewritten as:

select id, count(customer_id) 
from customers
where is_paid = True;

Example of SQL Snippets

If you have a normalized data schema with two tables: accounts and users with the following structure:

create table accounts (
    id integer not null primary key,
    created_at datetime not null,
    name varchar(255)
)

create table users (
    id integer not null primary key
    created_at datetime not null,
    account_id varchar(37) not null references accounts (id),
    name varchar(255),
    email varchar(255),
)

You can create a snippet [account_users] that creates a denormalized table on the fly:

-- this is the [account_users] snippet
select * 
from accounts a
join users u
on a.id = u.account_id

and then in your chart query, you can re-use this snippet:

with account_users as (
    [account_users]
)
select ...

Next Generation SQL Data Analysis Write SQL, visualize data, and share your results. Onboarding forward-thinking teams now:

No spam, ever! Unsubscribe any time. Learn more about the product.

results matching ""

    No results matching ""