使用 TeraData 计算任期 SQL

Tenure Calculation with TeraData SQL

将此 T-SQL 转换为 TeraData SQL 是我遇到的最困难的时期。有人可以帮我解决这个问题吗?我真的很喜欢它。我不理解日期与 teradata 的转换。我超级困惑。

财政月份示例:开始:2020-22-01 - 结束:2020-21-01 此查询在他们开始那里的位置的财政月份内提供数据结果。示例:2019 年 8 月 21 日


   ,CASE WHEN DAY(POSITION_START_DT) BETWEEN 22 AND 31 
         THEN CAST(
               CONCAT(
               CAST(YEAR(POSITION_START_DT)AS VARCHAR(4)),
               CASE WHEN LEN(MONTH(POSITION_START_DT)) = 1 THEN '-0' ELSE '-'END,
               CAST(MONTH(POSITION_START_DT)AS VARCHAR(2)),
               '-21') 
               AS date)

          WHEN DAY(POSITION_START_DT) BETWEEN 1 AND 6 
          THEN CAST(
               CONCAT(
               CAST(YEAR(DATEADD(MONTH,-1,POSITION_START_DT))AS VARCHAR(4)),
               CASE WHEN LEN(MONTH(DATEADD(MONTH,-1,POSITION_START_DT))) = 1 THEN '-0' ELSE '-'END,
               CAST(MONTH(DATEADD(MONTH,-1,POSITION_START_DT))AS VARCHAR(2)),
               '-21') 
           AS date) 

         ELSE CAST(
               CONCAT(
                   CAST(YEAR(DATEADD(MONTH,0,POSITION_START_DT))AS VARCHAR(4)),
                   CASE WHEN LEN(MONTH(DATEADD(MONTH,0,POSITION_START_DT))) = 1 THEN '-0' ELSE '-'END,
                   CAST(MONTH(DATEADD(MONTH,0,POSITION_START_DT))AS VARCHAR(2)),
                   '-21')
               AS date) END AS FISCAL_START_MONTH_INSALES

Fiscal Tenure Bucket 使用“Fiscal_Start_Month_insales 字段并将员工分组到一个桶中。 桶: - 0_3_MONTHS - 4_6_MONTHS - 7_12_MONTHS - 13_24_MONTHS - 25_PLUS_MONTHS - 其他未知


    ,CASE  WHEN DATEDIFF(Day,FiscalStartMonthInSales,CURRENT_DATE)/30  >= 25 THEN '25_PLUS_MONTHS'
                       WHEN DATEDIFF(Day,FiscalStartMonthInSales,CURRENT_DATE)/30  BETWEEN 13 AND 24 THEN '13_24_MONTHS'
                       WHEN DATEDIFF(Day,FiscalStartMonthInSales,CURRENT_DATE)/30  BETWEEN 7 AND 12 THEN '7_12_MONTHS'
                       WHEN DATEDIFF(Day,FiscalStartMonthInSales,CURRENT_DATE)/30  BETWEEN 4 AND 6 THEN '4_6_MONTHS'
                       WHEN DATEDIFF(Day,FiscalStartMonthInSales,CURRENT_DATE)/30  BETWEEN 0 AND 3 THEN '0_3_MONTHS'
                       ELSE 'Unknown' END as PositionTenureBucket

干杯~ 亚伦

只是转换函数(并结合两种似乎给出相同结果的情况),第一部分似乎可以是

CASE WHEN EXTRACT(DAY FROM POSITION_START_DT) BETWEEN 1 AND 6
     THEN CAST(CAST(CAST(ADD_MONTHS(POSITION_START_DT,-1) AS FORMAT 'YYYY-MM') AS VARCHAR(7))||'-21' AS DATE FORMAT 'YYYY-MM-DD')
     ELSE CAST(CAST(CAST(POSITION_START_DT AS FORMAT 'YYYY-MM') AS VARCHAR(7))||'-21' AS DATE FORMAT 'YYYY-MM-DD')
     END AS FISCAL_START_MONTH_INSALES

也许第二部分只是

 ,CASE WHEN (CURRENT_DATE-FiscalStartMonthInSales)/30  >= 25 THEN '25_PLUS_MONTHS'
       WHEN (CURRENT_DATE-FiscalStartMonthInSales)/30   BETWEEN 13 AND 24 THEN '13_24_MONTHS'
       WHEN (CURRENT_DATE-FiscalStartMonthInSales)/30   BETWEEN 7 AND 12 THEN '7_12_MONTHS'
       WHEN (CURRENT_DATE-FiscalStartMonthInSales)/30   BETWEEN 4 AND 6 THEN '4_6_MONTHS'
       WHEN (CURRENT_DATE-FiscalStartMonthInSales)/30   BETWEEN 0 AND 3 THEN '0_3_MONTHS'
       ELSE 'Unknown' END as PositionTenureBucket

将 Fred 的第一部分简化为:

Cast(To_Char(CASE WHEN Extract(DAY From POSITION_START_DT) BETWEEN 1 AND 6 
                  THEN Add_Months(POSITION_START_DT,-1)
                  ELSE POSITION_START_DT
             END, 'yyyy-mm') || '-21' AS DATE) 

并将逻辑简化为减去 6 天,得到该月的 1 号并加上 20 天:

Trunc(POSITION_START_DT - 6, 'mon') + 20