简体   繁体   English

dbt Jinja 宏将列集传递给列表并执行计算和重命名

[英]dbt Jinja macro to pass set of columns to a list and perform calculations and renaming

I have a code below where:我在下面有一个代码:

  1. I am fetching all the relevant columns from two tables and renaming them to a similar keyword,我正在从两个表中获取所有相关列并将它们重命名为类似的关键字,

  2. bringing all the data into 1 row.将所有数据放入 1 行。

  3. running a macro to identify mismatch运行宏来识别不匹配

I would like to see the final names of each column as s1_xxx or s2_xxx .我希望看到每列的最终名称为s1_xxxs2_xxx s1_ indicates the column is from table 1 and s2_xxx indicates it is from table2 s1_表示该列来自表1s2_xxx表示来自表2

The problem is that I have to pass the columns as a tuple/list as where format('%t', (s1_id,s1_qty,s1_price)),= format('%t', (s2_id,s2_qty,s2_price)) .问题是我必须将列作为元组/列表传递为where format('%t', (s1_id,s1_qty,s1_price)),= format('%t', (s2_id,s2_qty,s2_price)) But there could be multiple columns, in my example I am just showing 3. So the code could go like:但是可能有多个列,在我的示例中我只显示 3。所以代码可以是 go,例如:

(s1_id,s1_qty,s1_price, s1_aa, s1_ab, s1_ac.......n) . (s1_id,s1_qty,s1_price, s1_aa, s1_ab, s1_ac.......n) In that case, it doesn't look that readable在那种情况下,它看起来不那么可读

with source1 as (
select 
b.id as s1_id,
b.qty as s1_qty,
a.price as s1_price
from <table> as a
,unnest <details> as b
 where b.status != 'canceled'
),

source2 as (
select 
un_id  as s2_id, 
acqty_ as s2_qty, 
price_per_unit as s2_price  
from <table2>
where city != 'delhi'
) ,

compare as (
select *
from source1 s1
full outer join source2 s2
on id = id_
where format('%t', (s1_id,s1_qty,s1_price)) != format('%t', (s2_id,s2_qty,s2_price))
)

Select 

*,   {{ create_calc(s1_price,s2_price) }} ,   {{ create_calc(s1_qty,s2_qty) }}

from compare
## this would then generate two columns:

**is_price_mismatch and is_qty_mismatch** based on the macro (see below)

{{ create_calc(col1,col2) }} indicates the following: {{ create_calc(col1,col2) }} 表示如下:

{% macro create_calc(col1, col2) -%}
if(ifnull({{ col1 }} != {{ col2 }},true) is true,1,0) as {{ col1 | replace(‘s1_,'is_') }}_mismatch
{% endmacro %}

I wanted to check if it is possible to use jinja to kind of define the columns in a list/tuple and then pass it in the where clause (see below):我想检查是否可以使用 jinja 来定义列表/元组中的列,然后将其传递到where子句中(见下文):

where format('%t', (s1_id,s1_qty,s1_price)) would convert to something like: where format('%t', (s1_id,s1_qty,s1_price))将转换为类似以下内容:

where format('%t', list) but even if I do this I will have to create two lists? where format('%t', list)但即使我这样做我也必须创建两个列表? as the names of the columns have different prefixes s1_xx, s2_xx:由于列的名称具有不同的前缀 s1_xx、s2_xx:

  1. col_list = (s1_id, s1_qty, s1_price,.......n)
  2. col_list2 = (s2_id, s2_qty, s2_price,.......n)

And then passing it to where clause:然后将其传递给where子句:

where format('%t', (col_list)),= format('%t', (col_list2))

or is there a way I can pass one list and rename the columns along with it via some code?或者有没有一种方法可以传递一个列表并通过一些代码重命名列?

col_list = (id, qty, price) ## note the the col_list = (id, qty, price) ## 注意

final output would then be:最终的 output 将是:

  • s1_id, s1_id,
  • s1_qty, s1_数量,
  • s1_price, s1_price,
  • s2_id, s2_id,
  • s2_qty, s2_数量,
  • s2_price s2_价格
  • is_price_mismatch is_price_mismatch
  • is_qty_mismatch is_qty_mismatch

I was hoping someone can help me how I can code this logic or maybe any other solution that would work with my existing code above / minor adjustments我希望有人能帮助我如何编写这个逻辑或者任何其他可以与我上面的现有代码/小调整一起使用的解决方案

Update:更新:

I tried the following logic:我尝试了以下逻辑:

{% set cols = ("price", "qty") %}

and passed it to my format() :并将其传递给我的format()

format('%t',{{ cols }}) as serial

but the results are not correct somehow the ' is being attached to the beginning and ending of the column names: (can be seen in the compiled version:但结果不正确'被附加到列名的开头和结尾:(可以在编译版本中看到:

format('%t',('price', 'quantity')) as serial

what i need is:我需要的是:

format('%t',(price, quantity)) as serial

it can be solved using two ways:它可以通过两种方式解决:

{% set cols = ('id' , 'qty') %}


## and then using 

format('%t',{{ cols_ | replace('\'','') }})



or要么


{% set cols = 'id,quantity' %}


## and then using : 

format('%t',({{ cols_ }})) as d365_cols



声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM