Problem
I have a table which holds different products and I need to dynamically create a view for each product in the table. For example, if 3 new products is added to the table, I need to create 3 new views.
Currently I have a macro to run a query on the table for all distinct products and then iterate over them, creating views.
Simplified Code:
{% macro create_consumption_views() %}
{% set products = run_query(updated_products_query) %}
{% for product in products %}
CREATE OR REPLACE VIEW `{{ view_name }}`
AS SELECT * FROM {{ ref('my table') }}
WHERE product = "{{ product }}"
{% endfor %}
{% endmacro %}
Is there a more idiomatic way to do this? I have heard that DDL statements in dbt aren’t generally recommended since it tries to abstract these away.