Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

call block with arguments #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions analysis/demo.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% set cols = dbtplyr.get_column_names( source('chinook','albums') ) %}
{% set cols_n = dbtplyr.ends_with('Id', cols) %}
{% set cols_ind = dbtplyr.ends_with('Id', cols) %}

select

{% call(var) dbtplyr.across(cols_n) %}
sum({{var}}) as {{var}}_tot
{% endcall %},
{% call(var) dbtplyr.across(cols_ind) %}
avg({{var}}) as {{var}}_avg
{% endcall %}

from {{ source('chinook','albums') }}
10 changes: 10 additions & 0 deletions analysis/src_analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2

# Uses SQLite: https://github.com/codeforkjeff/dbt-sqlite/blob/main/README.md
sources:
- name: chinook
schema: main
database: main
tables:
- name: albums
- name: artistss
4 changes: 2 additions & 2 deletions macros/across.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% macro across(var_list, script_string, final_comma) %}
{% macro across(var_list, final_comma) %}

{% for v in var_list %}
{{ script_string | replace('{{var}}', v) }}
{{ caller(v) }}
{%- if not loop.last %},{% endif %}
{%- if loop.last and final_comma|default(false) %},{% endif %}
{% endfor %}
Expand Down