我需要帮助使用teradata(版本:16.0+)olap函数构造查询,下面的场景用于比较和消除teradata表中的汇总重复项。
我在表中有9条以下的记录。
Existing Data(Table - ABC)
ACCOUNT_ID EXT_REF_NO SERIAL_NUM RECORD_START_DT RECORD_END_DT
1 100000000002195 8495752450757852 341FE4E6A1AF 8/13/2019 12:24:42 8/20/2019 23:59:59
2 100000000002195 8495752450757852 342FE4E6A1AF 8/21/2019 08:49:08 8/25/2019 23:59:59
3 100000000002195 8495752450757852 343FE4E6A1AF 8/27/2019 02:42:46 8/26/2019 23:59:59
4 100000000002195 8495752450757852 344FE4E6A1AF 8/28/2019 06:33:50 8/28/2019 23:59:59
5 100000000002195 8495752450757852 345FE4E6A1AF 8/30/2019 02:35:32 8/31/2019 23:59:59
6 100000000002195 8495752450757852 346FE4E6A1AF 9/2/2019 00:25:05 9/1/2019 23:59:59
7 100000000002195 8495752450757852 347FE4E6A1AF 9/3/2019 03:33:28 9/3/2019 23:59:59
8 100000000002195 8495752450757852 348FE4E6A1AF 9/4/2019 18:35:45 9/8/2019 23:59:59
9 100000000002195 8495752450757852 349FE4E6A1AF 9/10/2019 11:22:54 3/16/2020 23:59:59
Output
ACCOUNT_ID EXT_REF_NO SERIAL_NUM RECORD_START_DT RECORD_END_DT
1 100000000002195 8495752450757852 341FE4E6A1AF 8/13/2019 12:24:42 8/26/2019 23:59:59
2 100000000002195 8495752450757852 342FE4E6A1AF 8/28/2019 06:33:50 8/28/2019 23:59:59
3 100000000002195 8495752450757852 343FE4E6A1AF 8/30/2019 02:35:32 9/1/2019 23:59:59
4 100000000002195 8495752450757852 345FE4E6A1AF 9/3/2019 03:33:28 9/8/2019 23:59:59
5 100000000002195 8495752450757852 346FE4E6A1AF 9/10/2019 11:22:54 3/16/2020 23:59:59
记录结束日期应始终大于记录开始日期
我们只考虑当前行的记录开始日期=(记录结束日期+1天)上一行的记录,如果差异超过1天则不考虑
您可以发现第3行和第6行违反了第1点,这实际上是对当天过期记录的一个错误,而在数据输入完成时,您实际上可以将第3行和第6行的记录开始日期分别视为8/26/2019 00:00和9/2/2019 00:00:00进行计算
帐户id、分机参考号、序列号三者都应考虑按分区
我试过下面这样的方法。仅获取一行输出,最小设备开始日期和最大设备结束日期如下:
账户号扩展号参考序列号设备开始日期设备结束日期10000000002195 8495752450757852 341fe4e6a1af 8/13/2017 12:24:42.000000 9/16/2017 23:59:59.000000
Query: SELECT
ACCOUNT_ID,
EXT_REF,
SERIAL_NUM,
CASE WHEN (B.DIFF_DAYS <= 1 OR B.DIFF_DAYS IS NULL) THEN
min(DEVICE_START_DATE)
OVER (PARTITION BY ACCOUNT_ID,EXT_REF,SERIAL_NUM order by
DEVICE_END_DATE desc)
WHEN (B.DIFF_DAYS > 1 ) THEN
min(DEVICE_START_DATE)
OVER (PARTITION BY ACCOUNT_ID,EXT_REF,SERIAL_NUM order by
DEVICE_END_DATE desc)
END AS DEVICE_START_DATE,
DEVICE_END_DATE
FROM
(SELECT A.ACCOUNT_ID,
A.EXT_REF,
A.SERIAL_NUM,
A.DEVICE_START_DATE,
A.DEVICE_START_DATE_VIRTUAL,
A.DEVICE_END_DATE,
MIN(A.DEVICE_END_DATE)
OVER ( PARTITION BY A.ACCOUNT_ID,A.EXT_REF,A.SERIAL_NUM ORDER BY A.DEVICE_END_DATE
ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) AS DEVICE_END_DATE_PREVIOUS_ROW,
TRUNC(A.DEVICE_START_DATE_VIRTUAL) - TRUNC(DEVICE_END_DATE_PREVIOUS_ROW) AS DIFF_DAYS
FROM
(SELECT
ACCOUNT_ID,
EXT_REF,
SERIAL_NUM,
DEVICE_START_DATE,
CASE WHEN DEVICE_START_DATE > DEVICE_END_DATE
THEN (DEVICE_START_DATE - INTERVAL '1' DAY)
ELSE DEVICE_START_DATE END AS DEVICE_START_DATE_VIRTUAL,
DEVICE_END_DATE
FROM NDW_XH_TEMP_TABLES.TEST) A) B
QUALIFY
ROW_NUMBER()
OVER (PARTITION BY ACCOUNT_ID,EXT_REF,SERIAL_NUM order by DEVICE_END_DATE desc) = 1;
1条答案
按热度按时间nmpmafwu1#
如果需要嵌套的olap函数,则应按预期工作:
这将查找差距,在应用flag=1后,当前行获得最大结束日期,而上一行获得匹配的开始日期。外部选择最后将此开始日期添加到当前行。