小编典典

teradata,重置时间,分区依据,排序依据

sql

我需要帮助来了解以下代码。在Teradata中使用时,我从未见过重置。RESET
WHEN在Teradata中做什么?我了解分区和顺序的部分。我也不确定为什么没有按PARTITION BY
A.ACCT_DIM_NB,A.DAY_TIME_DIM_NB ORDER BY
A.TXN_POSTING_SEQ对分区进行分区。另外,仅在整个分区窗口中,行数在未绑定的前导和当前行之间行吗?

Removed

阅读 235

收藏
2021-03-17

共1个答案

小编典典

我也不确定为什么没有按PARTITION BY Y.ACCT_DIM_NB,Y.DAY_TIME_DIM_NB或ORDER BY
Y.DAY_TIME_DIM_NB,Y.TXN_POSTING_SEQ对分区进行分区

不知道,但这会返回不同的结果(并且Y.DAY_TIME_DIM_NB不需要,ORDER BY因为它已经被它分区了)

另外,仅在整个分区窗口中,行数在未绑定的前导和当前行之间行吗?

与完全相同ROWS UNBOUNDED PRECEDING,即“累积最大值”的语法变体。分区是ROWS UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING

RESET WHEN在Teradata中做什么?

RESET WHEN是用于动态添加分区的Teradata扩展,它是两个(在您的情况下)或三个嵌套的OLAP函数的较短语法:

-- using RESET WHEN
MAX(A.RUN_BAL_AM)
OVER (PARTITION BY A.ACCT_DIM_NB
      ORDER BY A.DAY_TIME_DIM_NB, A.TXN_POSTING_SEQ 
      RESET WHEN A.CS_TXN_CD NOT IN ('072','075','079','107','111','112','139','181','318') 
      ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS  EOD_BAL_AM



-- Same result using Standard SQL
SELECT  
   Max(A.RUN_BAL_AM)
   Over (PARTITION BY A.ACCT_DIM_NB, dynamic_partition
         ORDER BY A.DAY_TIME_DIM_NB, A.TXN_POSTING_SEQ 
         ROWS BETWEEN Unbounded Preceding AND CURRENT ROW) AS  EOD_BAL_AM

FROM 
 ( 
   SELECT
      -- this cumulative sum over 0/1 assigns a new value for each series of rows based on the CASE
      Sum(CASE WHEN A.CS_TXN_CD NOT IN ('072','075','079','107','111','112','139','181','318') THEN 1 ELSE 0 end)
      Over (PARTITION BY A.ACCT_DIM_NB, dynamic_partition
            ORDER BY A.DAY_TIME_DIM_NB, A.TXN_POSTING_SEQ 
            ROWS Unbounded Preceding) AS dynamic_partition
   FROM ...
 ) AS dt
2021-03-17