【发布时间】:2018-12-05 16:22:56
【问题描述】:
下面是我之前的问题的链接。 Retain values till there is a change in value in 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;
谢谢 桑迪
【问题讨论】:
-
你能在你的 CTE 中添加一个 where 子句用于你想要扩展的那些,然后合并你不想扩展的那些吗?
-
嗨@Andrew。感谢您的回复。我只想在数据中保留其他几列。如果我包含它,我会收到一条错误消息 'inbound must be less than outer bound' 。如果我不包括它似乎很好。如何纠正这个问题?谢谢。
标签: teradata teradata-sql-assistant