【问题标题】:How to create macro variable in SQL/Teradata?如何在 SQL/Teradata 中创建宏变量?
【发布时间】:2017-05-20 16:54:16
【问题描述】:

我写了一个简单的查询,它每 3 天汇总每个用户 ID 的交易金额,

从 transaction_table 中选择 user_id, sum(tran_amt) 作为 tot_amt
其中 tran_dt>=cast('2016-12-31' as date) - INTERVAL '2' DAY 和 tran_dt

我想计算从 12/01 到 12/31 整个月的 3 天累计交易金额。我知道如何在 SAS 中做到这一点,只需将日期 '2016-12-31' 替换为宏变量,例如 &tera_dt.,类似这样

%do i=1 %to 31;

调用 symput('tera_dt', "'"||put(intnx('day','1Jan2017'd,-&i,'b'),yymmdd10.)||"'")

但是如何在 Teradata 中创建此日期宏变量?谢谢!


或者换一种说法,如何在 Teradata 中创建变量列表?我想创建一个名为 tera_dt 的宏变量,该变量包含从 '2016-10-01' 到 '2016-10-31' 总共 31 个日期的日期,然后我将对这个宏变量 tera_dt 运行查询。谢谢!

【问题讨论】:

    标签: mysql sql macros teradata


    【解决方案1】:

    我认为您不需要为这个问题生成代码。您可以加入针对系统视图sys_calendar.calendar 的查询。像这样的:

    select a.user_id
         , b.calendar_date as date
         , sum(a.tran_amt) as tot_amt
    from transaction_table a
    inner join 
      (select calendar_date from sys_calendar.calendar
      where year_of_calendar=2016 and month_of_year=12) b
    on a.tran_dt>=b.calendar_date - INTERVAL '2' DAY
      and a.tran_dt<=b.calendar_date
    group by 1,2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-10
      • 2023-04-08
      • 2019-09-25
      • 2020-05-31
      • 2015-10-21
      • 2013-10-02
      • 2017-10-04
      • 2013-01-23
      相关资源
      最近更新 更多