Data Engineering with dbt by Roberto Zagni

Data Engineering with dbt by Roberto Zagni

Author:Roberto Zagni
Language: eng
Format: epub
Publisher: Packt Publishing Pvt. Ltd.
Published: 2023-06-29T00:00:00+00:00


How to write a macro

Writing a macro is as simple as writing your SQL inside a macro declaration in a SQL file inside the macro folder, and it would look like the following:

{% macro this_with_filter(arg1, arg2='XXXX') %} SELECT * FROM {{this}} {% if arg1 %} WHERE {{arg1}} = {{arg2}} {% endif %} {% endmacro %}

In the preceding piece of code, we have the following:

A macro named this_with_filter declared with two arguments.

An arg1 argument, which is mandatory, as it does not have a default value.

An arg2 argument, which is optional, as it does have the default value of the string XXXX.

The body of the macro, which is made of four lines that mix normal SQL and Jinja code, which looks very much like Python.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.