From 7f7848b824525f185ce031e72a5287eac1844891 Mon Sep 17 00:00:00 2001 From: Dan Houghton Date: Tue, 10 Aug 2021 18:29:01 +0100 Subject: [PATCH 1/2] use a call block to insert user sql --- macros/across.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/macros/across.sql b/macros/across.sql index 9ebcfa4..c9abd0a 100644 --- a/macros/across.sql +++ b/macros/across.sql @@ -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 %} From 0fa33c73bea73a7305bf7411b3684ef71e94505f Mon Sep 17 00:00:00 2001 From: Dan Houghton Date: Tue, 10 Aug 2021 18:31:39 +0100 Subject: [PATCH 2/2] demo - uses sqlite's chinook db --- analysis/demo.sql | 14 ++++++++++++++ analysis/src_analysis.yml | 10 ++++++++++ 2 files changed, 24 insertions(+) create mode 100644 analysis/demo.sql create mode 100644 analysis/src_analysis.yml diff --git a/analysis/demo.sql b/analysis/demo.sql new file mode 100644 index 0000000..9c90f8a --- /dev/null +++ b/analysis/demo.sql @@ -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') }} \ No newline at end of file diff --git a/analysis/src_analysis.yml b/analysis/src_analysis.yml new file mode 100644 index 0000000..b8a2cbb --- /dev/null +++ b/analysis/src_analysis.yml @@ -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 \ No newline at end of file