仅为 teradata 中的某些数据部分保留值

Retain values only for certain section of data in teradata

下面是我上一个问题的link。

它按照社区成员之一@Dnoeth 的建议工作。是否可以仅针对特定部分数据进行此保留?

即,仅保留 Dep 为 A 或 B 的数据。当 Dep 为 C 时,只需使用与输入相同的值,无需保留到特定值。

Data:
Cust_id Balance st_ts          Dep
123     1000    27MAY2018 A
123     350     31MAY2018  A
256     2000   29MAY2018  B
345     1000   28APR2018   C
345     1200   26MAY2018   C

输出要求:

Cust_id Balance st_ts         Dep
123     1000    27MAY2018 A
123     1000    28MAY2018 A
123     1000    29MAY2018 A
123     1000    30MAY2018 A
123     350     31MAY2018  A
256     2000   29MAY2018  B
256     2000   30MAY2018  B
256     2000    31MAY2018 B
345     1000   28APR2018   C
345     1200   26MAY2018   C

使用的查询:

Wth cte
{
  SELECT customer_id, bal, st_ts,
      -- return the next row's date
      Coalesce(Min(st_ts)
               Over (PARTITION BY customer_id 
                     ORDER BY st_ts
                     ROWS BETWEEN 1 Following AND 1 Following)
              ,Date '2018-06-01') AS next_Txn_dt
   FROM BAL_DET;
}
SELECT customer_id, bal
  ,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(ST_TS, next_Txn_dt) AS pd;

谢谢 桑迪

您可以添加一个 CASE 来检查 Dep = 'C':

WITH cte AS 
(  SELECT customer_id, bal, st_ts, dep,
      -- return the next row's date
      CASE
        WHEN dep = 'C' 
           THEN st_ts +1 -- simply increase date
        ELSE
           Coalesce(Min(st_ts)
                    Over (PARTITION BY customer_id 
                          ORDER BY st_ts
                          ROWS BETWEEN 1 Following AND 1 Following)
                   ,DATE '2018-06-01')
      END AS next_Txn_dt
   FROM BAL_DET
)
SELECT customer_id, bal
  ,Last(pd) -- last day of the period
  ,dep
FROM cTE
-- make a period of the current and next row's date
-- and return one row per day
EXPAND ON PERIOD(ST_TS, next_Txn_dt) AS pd