0

以下是我之前的问题的链接。 保留值直到 Teradata 中的值发生变化

它按照社区成员之一@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;

谢谢桑迪

4

1 回答 1

0

您可以添加一个 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
于 2018-06-27T21:43:24.107 回答