我需要将第一行中的日期(结束dt)与secord行中的日期(strt dt)进行比较,如果匹配,我需要选择第二行的strt dt。 如果它匹配,我需要拿起第一排的strt dt。一个订阅和频道包可以有多个行。 我发现在teradata中编写查询以获得所需的输出有困难。
以下是案例和预期结果。
案例1
Subscription Channel package Start dt End dt
11111112 Sports 7/3/2015 11/28/2015
11111112 Sports 4/1/2016 11/23/2016
11111112 Sports 11/23/2016 12/17/2017
需要输出
Subscription Channel package Start dt End dt
11111112 Sports 4/1/2016 12/17/2017
案例2
Subscription Channel package Start dt End dt
11111112 Sports 7/3/2015 11/28/2015
11111112 Sports 11/28/2015 4/1/2016
11111112 Sports 4/1/2016 11/23/2016
11111112 Sports 11/23/2016 12/17/2017
需要输出
Subscription Channel package Start dt End dt
11111112 Sports 7/3/2015 12/17/2017
我用
尝试了MIN(Start dt) OVER (PARTITION BY Subscription , Channel package
ORDER BY Start dt ROWS BETWEEN 1 FOLLOWING AND 1 FOLLOWING
) AS NXT Start dt
但是当我在使用qualify时面临一些问题时,我无法继续前进。
答案 0 :(得分:0)
我知道这可能是一个较晚的解决方案,也可能不是最快的解决方案。 我会这样开始:
(
SELECT
TBL.SUBSCRIPTION
, TBL.CHANNEL
, TBL.SUBSCRIPTION || TBL.CHANNEL AS SUB_CHANNEL
, CASE WHEN TBL.END_DT = TBL.START_DT THEN 1 ELSE 0 END AS SAME_DAY_FLAG
, Dense_Rank() Over (ORDER BY SUB_CHANNEL ) AS GRP_RANK
, TBL.START_DT
, TBL.END_DT
, PERIOD(TBL.START_DT, CASE WHEN TBL.END_DT > DATE THEN DATE ELSE TBL.END_DT + 1 END) AS COVERD_DTES
FROM <YOUR_TABLE> AS TBL
) WITH DATA
ON COMMIT PRESERVE ROWS;
根据需要创建索引。创建 SUB_CHANNEL
的原因是因为一些愚蠢的原因我无法解决,我无法为下面的某些功能拉出多于一列。
/* Create a table that will contain the Merged rows with proper start and end dates combining adjacent periods*/
CREATE VOLATILE TABLE VT_TBL_MERGED_ROWS AS
(
WITH CTE (GRP_RANK, COVERD_DTES) AS
(
SELECT
GRP_RANK, COVERD_DTES
FROM VT_PRE_CTE
)
SELECT * FROM TABLE
(
TD_SYSFNLIB.Td_Normalize_Overlap_Meet(NEW VARIANT_TYPE( CTE.GRP_RANK), CTE.COVERD_DTES)
RETURNS (GRP_RANK INT, COVERD_DTES PERIOD(DATE))
HASH BY GRP_RANK
LOCAL ORDER BY GRP_RANK, COVERD_DTES
) AS TBL_ROWS
) WITH DATA
PRIMARY INDEX (GRP_RANK);
函数 TD_SYSFNLIB.Td_Normalize_Overlap_Meet
对重叠的结束日期和开始日期进行标准化,并分别为您提供最低和最后开始日期和结束日期的单行。由于我无法理解传递多个列的方法,因此我创建了 GRP_RANK
,然后可以将其绑定。
CREATE VOLATILE TABLE VT_PRE_OUTPUT AS
(SELECT
A.SUBSCRIPTION
, A.CHANNEL
, A.GRP_RANK
, B.COVERD_DTES
, Min(A.EFF_DT) AS EFF_DT
, Max(A.EXP_DT) AS EXP_DT
, End(B.COVERD_DTES)- Begin(B.COVERD_DTES) AS TOTAL_DAYS
FROM VT_PRE_CTE AS A
JOIN VT_TBL_MERGED_ROWS AS B
ON A.GRP_RANK = B.GRP_RANK
AND A.COVERD_DTES OVERLAPS B.COVERD_DTES
GROUP BY 1,2,3)
WITH DATA
PRIMARY INDEX(MTN, EXP_DT)
ON COMMIT PRESERVE ROWS;
如果 A.COVERD_DTES OVERLAPS B.COVERD_DTES
组合之间有中断,这个 SUBSCRIPTION/CHANNEL
实际上会给你多行。
您可以决定如何处理多个片段,使用 QUALIFY Row_Number() OVER(PARTITION BY SUBSCRIPTION, CHANNEL ORDER BY MIN_DT ASC) = 1
获取第一次出现或使用 ORDER BY MIN_DT DESC)
获取最新出现。