sql嵌套的case语句teradata将时间戳(0)、时间戳(6)转换为日期并进行比较

mnowg1ta  于 2021-08-13  发布在  Java
关注(0)|答案(1)|浏览(384)
I want to generate qualify_flag as '1' always when device_end_dt = '9999-12-31 23:29:59'. If it is 
 not '9999-12-31 23:29:59' then if DEVICE_END_DT + 1 day = CREATION_DATE then qualify_flag is 0 else 1. 
DEVICE_END_DT is TIMESTAMP(0), CREATION_DATE  is TIMESTAMP(6). 
CREATION_DATE column doesn't have any value which is '9999-12-31'.

下面的case语句失败,日期无效,看起来像是'9999-12-31'不知怎么来了,'9999-12-31'+1)模棱两可。

select
case When 
        Cast(DEVICE_END_DT AS DATE) <> CAST('9999-12-31' AS DATE) 
            THEN
                case when CAST((Cast(DEVICE_END_DT AS DATE) + 1) AS DATE) = Cast(CREATION_DATE AS DATE)
                        then 0
                        else 1
                end

case when   
        Cast(DEVICE_END_DT AS DATE) = CAST('9999-12-31' AS DATE) 
            THEN
                            1 
end 
end as qualify_flag
 FROM ABC;
7z5jn7bk

7z5jn7bk1#

这就是你的逻辑:

CASE 
  WHEN Cast(DEVICE_END_DT AS DATE) = Cast(CREATION_DATE AS DATE) - 1
  THEN 0
  ELSE 1
END

顺便说一句,最好使用日期文字 DATE '9999-12-31' 而不是 CAST('9999-12-31' AS DATE) .
你想解决的最后一个商业问题是什么?看起来像是在缓慢变化的维度中识别坏数据。

相关问题