[英]Case Statements - How to subtract n years from specific data/time field in Redshift
I Need your help regarding this case statement that has be stumped.关于这个被难倒的案例陈述,我需要你的帮助。
I have a field called "activity_end_date_time".我有一个名为“activity_end_date_time”的字段。 This field is a date/time field i am trying to write a simple case statement to say
该字段是一个日期/时间字段,我正在尝试编写一个简单的案例陈述来说明
My thinking is我的想法是
Todays date = 26/01/2023- 1095 days (3 years) = 27/01/2020 anything prior to 27/01/2020 should be "More than 3 years"今天的日期 = 26/01/2023- 1095 天(3 年)= 27/01/2020 27/01/2020 之前的任何内容都应为“超过 3 年”
However i have examples where Last_Activity_End_Date is "2018-12-01", however my case statement is returning "3 Years"但是我有一些例子,其中 Last_Activity_End_Date 是“2018-12-01”,但是我的案例陈述返回“3 年”
This is my case statement这是我的案例陈述
case
when Last_Activity_End_Date < Dateadd(year,3,Last_Activity_End_Date ) then '3 Years'
when Last_Activity_End_Date >= Dateadd(year,+3,Last_Activity_End_Date ) then 'More than 3 Years'
when Last_Activity_End_Date IS null THEN 'NULL'
end as "Last_Activity_Identifer"
Looking forward to your help期待您的帮助
Because Amazon Redshift is based on PostgreSQL, you can try to use PostgreSQL function age
in next way:因为 Amazon Redshift 是基于 PostgreSQL 的,你可以尝试使用 PostgreSQL function
age
来下一个方法:
select
case
when age(Last_Activity_End_Date) < '3 year' then '3 Years'
when age(Last_Activity_End_Date) >= '3 year' then 'More than 3 Years'
end as "Last_Activity_Identifer";
test it online: https://sqlize.online/s/lt在线测试: https://sqlize.online/s/lt
Your logic is comparing the column to itself.您的逻辑是将列与自身进行比较。 You need to compare to 3 year before today's date.
您需要与今天日期之前的 3 年进行比较。 Using your structure and comparison operators:
使用您的结构和比较运算符:
case
when getdate() < Dateadd(year,3,Last_Activity_End_Date ) then '3 Years'
when getdate() >= Dateadd(year,3,Last_Activity_End_Date ) then 'More than 3 Years'
when Last_Activity_End_Date IS null THEN 'NULL'
end as "Last_Activity_Identifer"
This is untested but should work and shows the logic needed for the comparision.这是未经测试的,但应该可以工作并显示比较所需的逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.