1

我正在尝试使用撇号旋转一列,但这在 DBT 中非常困难。有任何想法吗?我尝试了双引号,但 dbt 没有选择它,我不能在数据透视表中使用 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 %})
4

3 回答 3

1
{% 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

来自 DBT 的 Christine。Jinja2 似乎有很多其他语言处理它的方式引用的问题=/。

于 2020-06-25T12:56:54.527 回答
1

在 DBT 宏中试试这个链接。有一个仅用于枢轴的宏。

https://github.com/fishtown-analytics/dbt-utils/blob/master/macros/sql/pivot.sql

将值从行旋转到列。

例子:

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()

论据:

  • column:列名,必填
  • values:要转换为列的行值列表,必填
  • alias:是否创建列别名,默认为True
  • agg:SQL聚合函数,默认为sum
  • cmp:SQL值比较,默认为=
  • prefix:列别名前缀,默认为空
  • suffix:列别名后缀,默认为空
  • then_value:比较成功时使用的值,默认为 1
  • else_value:比较失败时使用的值,默认为 0
  • 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 %}
于 2020-06-24T13:24:09.110 回答
0

如果他们想坚持自己的查询,我怀疑换掉

'承包商'错误和遗漏cover_rated_premium'

使用“承包商的错误和遗漏cover_rated_premium”,您使用“

而不是 ' 可能会解决它?但不是 100% 确定这里是否还有其他问题。我确实认为 utils 的 pivot 宏应该很好用!只是不确定它是否能很好地处理 ' (我认为应该)。他们也无法在与枢轴相同的步骤中重命名列(这就是我认为在这里发生的事情),但他们可以轻松使用枢轴函数,然后在随后的 CTE 中重命名列

来自 DBT 的 Andrew

于 2020-06-25T12:46:29.440 回答