【问题标题】:create a variable to see date when model was run with dbt创建一个变量以查看使用 dbt 运行模型的日期
【发布时间】:2022-01-18 01:15:48
【问题描述】:

我需要在我的 dbt 模型中添加一个列,该列显示模型运行的当前日期。

我已经尝试将{{ run_started_at.strftime("%Y-%m-%d") }} 直接添加到我的模型中,还尝试设置一个名为 date 的变量并创建这段代码 ({% set report_date = dbt_utils.get_query_results_as_dict("select dateadd(day,-1,current_date()) as col")["COL"][0] %}),但问题是日期,就像我们今天在2021-12-14,该列的结果是 1995(这是2021 - 12 - 14 的减法)。那么,现在有人可以将这个变量转换为日期吗?谢谢!

【问题讨论】:

    标签: date variables casting dbt


    【解决方案1】:

    我想我现在知道这个秘密了

    请记住 dbt 是一个类似代码生成的工具

    在你的情况下,我猜你必须将你的 jinja 代码包装在字符串引号中

    错误:

    SELECT {{ run_started_at.strftime("%Y-%m-%d") }} as YourColumn
    --compiled: SELECT 2021-12-14 as YourColumn
    --YourColumn=1995
    

    正确:

    SELECT '{{ run_started_at.strftime("%Y-%m-%d") }}' as YourColumn
    --compiled: SELECT '2021-12-14' as YourColumn
    --YourColumn=2021-12-14
    

    【讨论】:

      猜你喜欢
      • 2023-01-20
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-28
      • 2020-01-17
      • 2021-12-23
      • 1970-01-01
      相关资源
      最近更新 更多