【问题标题】:Retain values only for certain section of data in teradata仅保留 teradata 中特定数据部分的值
【发布时间】: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


【解决方案1】:

您可以添加一个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

【讨论】:

  • 嗨@dnoeth。感谢您的回复。我只想在数据中保留其他几列。如果我包含它,我会收到一条错误消息 'inbound must be less than outer bound' 。如果我不包括它似乎很好。如何纠正这个问题?谢谢。
  • @Sandy:在不了解您的实际查询以及您的数据和预期结果的情况下,无法提供帮助。
猜你喜欢
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-17
  • 2016-09-02
  • 2019-12-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多