rw-book-cover

Metadata

Highlights

• Where performance permits, CTEs should perform a single, logical unit of work. • CTE names should be as verbose as needed to convey what they do. • CTEs with confusing or noteable logic should be commented with SQL comments as you would with any complex functions, and should be located above the CTE. • CTEs that are duplicated across models should be pulled out and created as their own models. (View Highlight)

Where applicable, opt for filtering within import CTEs over filtering within logical CTEs. This allows a developer to easily see which data contributes to the end result (View Highlight)

Aggregations should be executed as early as possible before joining to another table. (View Highlight)

Ordering and grouping by a number (eg. group by 1, 2) is preferred over listing the column names (see this rant for why). Note that if you are grouping by more than a few columns, it may be worth revisiting your model design. If you really need to, the dbt_utils.group_by function may come in handy. (View Highlight)

Joins should list the left table first (i.e., the table you’re joining data to) (View Highlight)