Coder Social home page Coder Social logo

dynamic_pivot_dbt's People

Contributors

vwsergewv avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

dynamic_pivot_dbt's Issues

Include ref macro

Hi there,
thanks for your great work on the macro! We are using it within our dbt project, however, we still want to be able to use the ref macro within. Otherwise the table the macro is applied has to be hardcoded, which within a dbt project is highly disadvantageous.

How about wrapping the ref macro around the parameter my_table like ref(my_table). The entire thing would look like that then:

{% macro dynamic_pivot (columns_to_show, pivot_column, aggregate_column, my_table) %}


{%- set my_query -%}
SELECT distinct {{pivot_column}}  FROM {{ ref(my_table) }} ;
{%- endset -%}

{%- set results = run_query(my_query) -%} 

{%- if execute -%}
{#- Return the first column -#}
{%- set items = results.columns[0].values() -%}
{%- else -%}
{%- set items = [] -%}
{%- endif %}



SELECT {{columns_to_show}} ,
{%- for i in items %}
sum("{{i}}_views") AS "{{i}}_views"
{%- if not loop.last %} , {% endif -%}
{%- endfor %}
FROM (
        SELECT  {{columns_to_show}},  
        {%- for i in items %}
        CASE WHEN {{pivot_column}} = '{{i}}'  THEN {{aggregate_column}} ELSE 0 END AS "{{i}}_views"
        {%- if not loop.last %} , {% endif -%}
        {%- endfor %}
        FROM {{ ref(my_table) }}   )
GROUP BY  {{columns_to_show}} 
{% endmacro %}

If you still want to give the possibility to define a static reference for my_table, you could wrap it within an additional if-else clause. What do yo think?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.