✂️

Snippets

Introduction

Snippets let you share small, reusable bits of SQL with your team.

Demo

Creating your first Snippet

To create a snippet hit command / ctrl + k → "Create snippet"

Working with dates is a great example, check out this snippet we use at SeekWell which creates three date formats from a timestamp

date_trunc('day', {{column_name}})::date as "{{alias}}_day",
date_trunc('week', {{column_name}})::date as "{{alias}}_week",
date_trunc('month', {{column_name}})::date as "{{alias}}_month"
image

Once you type {{ you'll see the variable added to "Defaults". Here you can add a commonly used value for the variable (e.g. created_on).

💡

Variables are optional, not adding variables will create a "static snippet"

Navigation

  • To create a snippet hit command / ctrl + k → "Create snippet"
  • To use a snippet from a block hit command / ctrl + ; (semicolon)
  • To view / edit / delete your snippets hit command / ctrl + k → "View snippet"
image

Library

SnippetLibrary

NameTagsSQL
addSubSelect
ansi
(select {{group_by_col}}, max({{max_col}}) max_{{max_col}} from {{table}} group by 1) as max_{{table}}
dayWeekMonth
postgres
date_trunc('day', {{column_name}})::date::text as "{{alias}}_day", date_trunc('week', {{column_name}})::date::text as "{{alias}}_week", date_trunc('month', {{column_name}})::date::text as "{{alias}}_month"
addWith
postgres
with {{alias}} as ( select from {{table_name}} )
Days between two dates
postgres
now()::date - some_date::date

Suggest a snippet here.

Other resources to jump start