sql—使用case语句比较前一行和当前行之间的时间戳数据,并根据数据的差异执行操作

p8ekf7hl  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(426)

我需要帮助使用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;
nmpmafwu

nmpmafwu1#

如果需要嵌套的olap函数,则应按预期工作:

SELECT 
   ACCOUNT_ID
  ,EXT_REF_NO
  ,SERIAL_NUM
  ,Coalesce(Lag(next_start)
            Over (PARTITION BY ACCOUNT_ID, EXT_REF_NO
                  ORDER BY next_start NULLS LAST)
           ,min_start) AS RECORD_START_DT

-- If your Teradata version doesn't support LAG/LEAD you must switch to the MAX version
--  ,Coalesce(Max(next_start)
--            Over (PARTITION BY ACCOUNT_ID, EXT_REF_NO
--                  ORDER BY next_start NULLS LAST
--                  ROWS BETWEEN 1 Preceding AND 1 Preceding)
--           ,min_start) AS RECORD_START_DT
  ,RECORD_END_DT  
FROM
 (
   SELECT
      ACCOUNT_ID
     ,EXT_REF_NO
     ,SERIAL_NUM
     ,RECORD_START_DT
     ,RECORD_END_DT

     -- to check for a gap
     ,Lag(fixed_start)
      Over (PARTITION BY ACCOUNT_ID, EXT_REF_NO
            ORDER BY fixed_start DESC) AS next_start
--     ,Max(fixed_start)
--      Over (PARTITION BY ACCOUNT_ID, EXT_REF_NO
--            ORDER BY fixed_start DESC
--            ROWS BETWEEN 1 Preceding AND 1 Preceding) AS next_start

     -- used in the outer COALESCE to get the min start for the 1st group
     ,Min(RECORD_START_DT)
      Over (PARTITION BY ACCOUNT_ID, EXT_REF_NO) AS min_start

     -- gap detection
     ,CASE WHEN Cast(RECORD_END_DT AS DATE) + 1 = Cast(next_start AS DATE) THEN 0 ELSE 1 END AS flag
   FROM
    ( -- fixing the bad data first
      SELECT t.*
        ,CASE WHEN RECORD_START_DT > RECORD_END_DT THEN RECORD_START_DT - INTERVAL '1' DAY ELSE RECORD_START_DT END AS fixed_start
      FROM tab AS t
    ) AS fixed_data
   QUALIFY flag = 1
 ) AS dt

这将查找差距,在应用flag=1后,当前行获得最大结束日期,而上一行获得匹配的开始日期。外部选择最后将此开始日期添加到当前行。

相关问题