From fd643dcf3c928427e5c90bbba5f54a2e1d8f9823 Mon Sep 17 00:00:00 2001 From: Elize Papineau Date: Fri, 7 Jan 2022 22:42:49 -0800 Subject: [PATCH 1/3] Update get_qu ery_results_as_dict example to demonstrate accessing columnar results as dictionary values --- README.md | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index e2f75b0c..e77a465a 100644 --- a/README.md +++ b/README.md @@ -658,17 +658,25 @@ This macro returns a dictionary from a sql query, so that you don't need to inte **Usage:** ``` --- Returns a dictionary of the users table where the state is California -{% set california_cities = dbt_utils.get_query_results_as_dict("select * from" ~ ref('cities') ~ "where state = 'CA' and city is not null ") %} +{% set sql_statement %} + select city, state from {{ ref('users) }} +{% endset %} + +{%- set places = dbt_utils.get_query_results_as_dict(sql_statement) -%} + select - city, -{% for city in california_cities %} - sum(case when city = {{ city }} then 1 else 0 end) as users_in_{{ city }}, -{% endfor %} - count(*) as total -from {{ ref('users') }} -group by 1 + {% for city in places['CITY'] | unique -%} + sum(case when city = '{{ city }}' then 1 else 0 end) as users_in_{{ city | replace(' ', '_') }}, + {% endfor %} + + {% for state in places['STATE'] | unique -%} + sum(case when state = '{{ state }}' then 1 else 0 end) as users_in_{{ state }}, + {% endfor %} + + count(*) as total_total + +from {{ ref('users') }} ``` ### SQL generators From 25ba1eacff9312850eb28147430d8a3185e0751d Mon Sep 17 00:00:00 2001 From: Elize Papineau Date: Wed, 12 Jan 2022 23:50:52 -0800 Subject: [PATCH 2/3] Use slugify in example --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e77a465a..bde53740 100644 --- a/README.md +++ b/README.md @@ -667,7 +667,7 @@ This macro returns a dictionary from a sql query, so that you don't need to inte select {% for city in places['CITY'] | unique -%} - sum(case when city = '{{ city }}' then 1 else 0 end) as users_in_{{ city | replace(' ', '_') }}, + sum(case when city = '{{ city }}' then 1 else 0 end) as users_in_{{ dbt_utils.slugify(city) }}, {% endfor %} {% for state in places['STATE'] | unique -%} From 727b9c9cb17e363974dc50b5a6081c2927507e22 Mon Sep 17 00:00:00 2001 From: Elize Papineau Date: Wed, 12 Jan 2022 23:52:07 -0800 Subject: [PATCH 3/3] Fix slugify example with dbt_utils. package prefix --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index bde53740..9570ee54 100644 --- a/README.md +++ b/README.md @@ -1051,7 +1051,7 @@ select order_id, {%- for payment_method in payment_methods %} sum(case when payment_method = '{{ payment_method }}' then amount end) - as {{ slugify(payment_method) }}_amount, + as {{ dbt_utils.slugify(payment_method) }}_amount, {% endfor %} ...