[英]Pivoting a column with an apostrophe in DBT
I am trying to pivot a column with an apostrophe but this is very difficult in DBT.我正在尝试 pivot 带有撇号的列,但这在 DBT 中非常困难。 Any ideas?
有任何想法吗? I tried double quotes but dbt does not pick this up, and I cannot use a like statement in a pivot.
我试过双引号,但 dbt 没有选择它,我不能在 pivot 中使用 like 语句。
{% set pvt_details=[
('General liability cover_rated_premium' , 'gl_premium')
, ('Contractors' errors and omissions cover_rated_premium','eo_premium') ] %}
WITH filtered AS (
SELECT
quote_id
, target
, premium_after_amount
from {{ source('acdc', 'chopin_quote_rating_steps') }} cqrs
WHERE target IN ({% for column in pvt_details %} '{{column[0]}}' {%- if not loop.last -%}
, {%- endif %}
{% endfor %})
AND action = 'initial_premium'
)
select *
from filtered
pivot(sum(premium_after_amount)
for target in ({% for column in pvt_details %} '{{column[0]}}' {%- if not loop.last -%} ,
{%- endif %}
{% endfor %}))
as p (quote_id,
{% for column in pvt_details %} {{column[1]}} {%- if not loop.last -%} , {%- endif
%}
{% endfor %})
Try this link in DBT macros.在 DBT 宏中尝试此链接。 There is a macro just for pivots.
有一个仅用于枢轴的宏。
https://github.com/fishtown-analytics/dbt-utils/blob/master/macros/sql/pivot.sql https://github.com/fishtown-analytics/dbt-utils/blob/master/macros/sql/pivot.sql
Pivot values from rows to columns. Pivot 个值从行到列。
Example:例子:
Input: `public.test`
| size | color |
|------+-------|
| S | red |
| S | blue |
| S | red |
| M | red |
select
size,
{{ dbt_utils.pivot('color', dbt_utils.get_column_values('public.test',
'color')) }}
from public.test
group by size
Output:
| size | red | blue |
|------+-----+------|
| S | 2 | 1 |
| M | 1 | 0 |
dbt_utils.pivot()
Arguments:
Arguments:
- column: Column name, required
column:列名,必填
- values: List of row values to turn into columns, required
values:要转换为列的行值列表,必需
- alias: Whether to create column aliases, default is True
alias:是否创建列别名,默认为True
- agg: SQL aggregation function, default is sum
agg: SQL聚合function,默认为sum
- cmp: SQL value comparison, default is =
cmp: SQL 值比较,默认为=
- prefix: Column alias prefix, default is blank
prefix:列别名前缀,默认为空
- suffix: Column alias postfix, default is blank
suffix:列别名后缀,默认为空
- then_value: Value to use if comparison succeeds, default is 1
then_value: 如果比较成功则使用的值,默认为 1
- else_value: Value to use if comparison fails, default is 0
else_value:比较失败时使用的值,默认为0
- quote_identifiers: Whether to surround column aliases with double quotes, default is true
quote_identifiers:是否用双引号将列别名括起来,默认为true
{% macro pivot(column,
values,
alias=True,
agg='sum',
cmp='=',
prefix='',
suffix='',
then_value=1,
else_value=0,
quote_identifiers=True) %}
{% for v in values %}
{{ agg }}(
case
when {{ column }} {{ cmp }} '{{ v }}'
then {{ then_value }}
else {{ else_value }}
end
)
{% if alias %}
{% if quote_identifiers %}
as {{ adapter.quote(prefix ~ v ~ suffix) }}
{% else %}
as {{prefix ~ v ~ suffix }}
{% endif %}
{% endif %}
{% if not loop.last %},{% endif %}
{% endfor %}
{% endmacro %}
{% set pvt_details=[
('General liability cover_rated_premium', 'gl_premium'),
("Contractor\\\'s errors and omissions cover_rated_premium", 'eo_premium')
] %}
select
concat_ws(' :: ',
{% for column in pvt_details %}
'{{ column[0] }}'
{%- if not loop.last -%}, {%- endif %}
{% endfor %}
) as column_selection
from {{ ref('reference_model') }}
limit 1
Collapse
From Christine at DBT.来自 DBT 的克里斯汀。 Jinja2 seems to have an issue with quoting in the way that a lot of other languages handle it =/.
Jinja2 似乎有很多其他语言处理它的引用方式的问题 =/。
if they want to stick their own query, I suspect swapping out如果他们想坚持自己的查询,我怀疑换掉
'Contractors' errors and omissions cover_rated_premium' '承包商'错误和遗漏 cover_rated_premium'
with "Contractors' errors and omissions cover_rated_premium" where you use "在您使用“承包商的错误和遗漏 cover_rated_premium”
instead of ' might fix it?而不是 ' 可能会修复它? but not 100% sure if anything else is causing issues here.
但不能 100% 确定这里是否还有其他问题。 I do think the pivot macro of utils should work great.
我确实认为 utils 的 pivot 宏应该工作得很好。 Just not sure if it'll handle the ' well (I think it should), They also won't be able to rename the column in the same step as the pivot (which is what I think is happening here) but they could easily use the pivot function, then rename the columns in a succeeding CTE
只是不确定它是否能很好地处理 '(我认为它应该),他们也无法在与 pivot 相同的步骤中重命名该列(我认为这是在这里发生的)但他们可以很容易使用 pivot 函数,然后重命名后续 CTE 中的列
From Andrew at DBT来自 DBT 的安德鲁
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.