在具有相同列名的 2 个 DBT 模型上应用连接

Apply joins on 2 DBT models which having same column names

我想在 2 个 DBT 模型上应用联接,这 2 个模型具有相同的列名。因此,当我尝试应用任何连接时。我收到以下错误。

column "rateplan_amendmenttype" specified more than once

这是我正在尝试的代码片段:

with a_in as (
     select * from {{source('dbt_alice', 'common_a')}}
)

select * from a_in cross join {{source('dbt_alice', 'common_c')}}

使用DBT.We的adapter函数可以实现结果

-- store the columns from a_in and b_in as a list in jinja
{%- set common_a_cols = adapter.get_columns_in_relation(source('dbt_alice', 'common_a')) -%}
{%- set common_c_cols = adapter.get_columns_in_relation(source('dbt_alice', 'common_c')) -%}

-- select every field, dynamically applying a rename to ensure there are no conflicts
select
  {% for col in common_a_cols %}
    common_a.{{col.name}} as a_{{col.name}},
  {% endfor %}
  {% for col2 in common_c_cols %}
    {% if not loop.last %}
     common_c.{{col2.name}} as b_{{col2.name}},
    {% else %}
      common_c.{{col2.name}} as b_{{col2.name}}
    {% endif %}
  {% endfor %}
from
  {{source('dbt_alice', 'common_a')}} as common_a
cross join
  {{source('dbt_alice', 'common_c')}} as common_c