Retain values till there is a change in value in Teradata

前端 未结 1 1587
陌清茗
陌清茗 2021-01-17 01:54

There is a transaction history table in teradata where balance gets changed only when there is a transaction Data as below:

Cust_id Balance Txn_dt
123     1         


        
相关标签:
1条回答
  • 2021-01-17 02:16

    There are several ways to get this result, the simplest in Teradata utilizes Time Series Expansion for Periods:

    WITH cte AS
     (
       SELECT Cust_id, Balance, Txn_dt,
          -- return the next row's date
          Coalesce(Min(Txn_dt)
                   Over (PARTITION BY Cust_id 
                         ORDER BY Txn_dt
                         ROWS BETWEEN 1 Following AND 1 Following)
                  ,Txn_dt+1) AS next_Txn_dt
       FROM tab
     ) 
    SELECT Cust_id, Balance
      ,Last(pd) -- last day of the period
    FROM cte
    -- make a period of the current and next row's date
    -- and return one row per day
    EXPAND ON PERIOD(Txn_dt, next_Txn_dt) AS pd
    

    If you run TD16.10+ you can replace the MIN OVER with a simplified LEAD:

    Lead(Txn_dt)
    Over (PARTITION BY Cust_id 
          ORDER BY Txn_dt)
    
    0 讨论(0)
提交回复
热议问题