Common Table Expressions (CTEs):

  1. Prerequisites for Writing dbt Models:

Once these prerequisites are met, you can start writing your dbt models, run them using the dbt run command, and see the transformed data in your database.

Jinja and Macros

In dbt, Jinja is a templating engine that allows for dynamic SQL generation, while macros are reusable pieces of SQL code that can be invoked in multiple places. Both Jinja and macros are essential components of dbt's functionality, enabling more modular and dynamic SQL code.

Example: Let's say you often need to format dates in a specific way in multiple models. Instead of writing the formatting SQL in each model, you can define a macro:

-- macros/date_formatting.sql
{% macro format_date(column_name) %}
    TO_CHAR({{ column_name }}, 'YYYY-MM-DD')
{% endmacro %}

Then, in your models, you can use this macro:

SELECT
    {{ format_date('created_at') }} as formatted_date,
    ...
FROM some_table

Macro example

  1. Macro for film_ratings.sql:

Create a new file in the macros/ directory of your dbt project, named film_ratings_macro.sql: