简体   繁体   English

案例陈述 - 如何从 Redshift 中的特定数据/时间字段中减去 n 年

[英]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该字段是一个日期/时间字段,我正在尝试编写一个简单的案例陈述来说明

  1. If there is activity in the last 3 years(from todays data) then "3 years"如果过去 3 年有活动(根据今天的数据),则“3 年”
  2. If there is activity more than last 3 years(from todays data) then "More 3 years"如果活动超过过去 3 年(根据今天的数据),则“超过 3 年”
  3. If there is no activity ie null then "Null"如果没有活动,即 null,则为“Null”

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.

相关问题 Redshift 如何从两个不同的日期列中减去分钟数 - Redshift how to subtract the number of minutes from two different date columns 如何将所有数据从一个红移集群复制到另一个红移集群 - How to copy all the data from one redshift cluster to another redshift cluster 如何编写触发器以将数据从 aurora 插入到 redshift - how to write a Trigger to insert data from aurora to redshift 将数据加载到 Amazon Redshift:忽略最后 n 行 - Loading data into Amazon Redshift: Ignore last n rows 如何从 Firestore (Android) 中的特定字段获取数据 - How to get data from a specific field in Firestore (Android) 如何通过文档 ID 从 firestore 网站获取特定字段数据 - How get specific Field data by document id from firestore website 如何将大型 Redshift 表划分为 N 个较小的表 - How to partition large Redshift table into N smaller ones 如何从自定义 REST API 将数据加载到 Redshift - How to load data into Redshift from a custom REST API 以特定分区格式将数据卸载到 redshift - Unload data into redshift in a specific partition format 如何在 firebase v9 的 firesrore 中为每个用户只更新一次特定字段的数据? - How to update a specific field of data only one time for each user in firesrore in firebase v9?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM