Oracle vs TDV 时间戳结果各不相同

Oracle vs TDV Time Stamp Results vary

我正在 Tibco 的 TDV 中构建用于 Spotfire 应用程序的逻辑,在其中,我有一些计算日期要求。下面的屏幕截图与 Select 语句完全相同,用于计算戴尔会计年度的开始时间,在 TDV 中,它给出的正确日期是 2020 年 2 月 1 日。但是 oracle 给出了不同的日期,我需要 oracle 验证在提升到 TDV 之前,所以这没有通过必需的验证步骤。我已经确认在 TDV 和 oracle developer 中都设置了所有默认格式。 如果还有更好的方法来确定财政开始日期,请分享。戴尔日期不是标准季度,每个只有 91 天,一年 364 天。去年的开始日期是 2019 年 2 月 2 日,所以今年是 2 月 1 日,下一个是 1 月 31 日。依此类推......我试图为 TDV 添加标签,但由于 < 1500 个代表点而无法添加。

Select 
to_Date('2019-02-02', 'YYYY-MM-DD')  + ((CAST(Current_Date -to_Date('2019-02-02', 'YYYY-MM-DD') AS INT)/364)* 364) 
From Dual

我不太清楚你想用这个逻辑做什么,但似乎不同之处在于 Oracle 不做 整数运算,而 TDV做。因此,在 TDV 中,表达式 (CAST(Current_Date -to_Date('2019-02-02', 'YYYY-MM-DD') AS INT)/364) 的计算结果似乎为 1,对吗?

但在 Oracle 中,INT 不是 first-class 数据类型 - INT 除以无类型数字是(浮点)NUMBER。表达式 (CAST(Current_Date -to_Date('2019-02-02', 'YYYY-MM-DD') AS INT)/364) 的计算结果为 1.85714285714286.

如果您希望 Oracle 将数字视为 INT,您必须自己截断它 - ROUND、FLOOR、CEIL 等。我认为此查询将执行您想要的操作。

Select 
to_Date('2019-02-02', 'YYYY-MM-DD') + FLOOR((Current_Date - to_Date('2019-02-02', 'YYYY-MM-DD'))/364)*364 
From Dual;

如果您需要将特定天数添加到日期中,您只需将该数字添加到日期中,它就会添加天数

SELECT TO_DATE ('2019-02-02', 'YYYY-MM-DD') + 364 FROM DUAL;

但是,与其在日期中添加天数 (364),不如使用 Oracle 的 ADD_MONTHS 函数更好。这还将考虑闰年,因此您无需将其纳入任何特殊逻辑。通过添加 12 个月,您可以将一年添加到一个日期,或者通过添加 3,您可以将一个季度添加到一个日期。使用此功能应该有助于使您的查询更加准确。

--Fiscal Year that will be starting this calendar year
SELECT ADD_MONTHS (TRUNC (CURRENT_DATE, 'Y') + 1, 1) AS start_date FROM DUAL;

--Fiscal Year start dates for last, current, and next calendar year
SELECT start_date,
       ADD_MONTHS (start_date, 12) - 1                    AS end_date,
       (ADD_MONTHS (start_date, 12) - 1) - start_date     AS number_of_days
  FROM (    SELECT ADD_MONTHS (ADD_MONTHS (TRUNC (CURRENT_DATE, 'Y') + 1, 1), 12 * (LEVEL - 2))    AS start_date
              FROM DUAL
        CONNECT BY LEVEL <= 3);

--Fiscal Year quarter start dates for last, current, and next calendar year
SELECT 'FY' || EXTRACT (YEAR FROM start_date)
           AS fiscal_year,
       ROW_NUMBER () OVER (PARTITION BY EXTRACT (YEAR FROM start_date) ORDER BY start_date)
           AS quarter_number,
       start_date,
       ADD_MONTHS (start_date, 3) - 1
           AS end_date,
       (ADD_MONTHS (start_date, 3) - 1) - start_date
           AS number_of_days
  FROM (    SELECT ADD_MONTHS (ADD_MONTHS (TRUNC (CURRENT_DATE, 'Y') + 1, 1), 3 * (LEVEL - 5))    AS start_date
              FROM DUAL
        CONNECT BY LEVEL <= 12);

输出

SQL> --Fiscal Year that will be starting this calendar year
SQL> SELECT ADD_MONTHS (TRUNC (CURRENT_DATE, 'Y') + 1, 1) AS start_date FROM DUAL;

   START_DATE
_____________
02-FEB-20

SQL>
SQL> --Fiscal Year start dates for last, current, and next calendar year
SQL> SELECT start_date,
  2         ADD_MONTHS (start_date, 12) - 1                    AS end_date,
  3         (ADD_MONTHS (start_date, 12) - 1) - start_date     AS number_of_days
  4    FROM (    SELECT ADD_MONTHS (ADD_MONTHS (TRUNC (CURRENT_DATE, 'Y') + 1, 1), 12 * (LEVEL - 2))    AS start_date
  5                FROM DUAL
  6          CONNECT BY LEVEL <= 3);

   START_DATE     END_DATE    NUMBER_OF_DAYS
_____________ ____________ _________________
02-FEB-19     01-FEB-20                  364
02-FEB-20     01-FEB-21                  365
02-FEB-21     01-FEB-22                  364

SQL>
SQL> --Fiscal Year quarter start dates for last, current, and next calendar year
SQL> SELECT 'FY' || EXTRACT (YEAR FROM start_date)
  2             AS fiscal_year,
  3         ROW_NUMBER () OVER (PARTITION BY EXTRACT (YEAR FROM start_date) ORDER BY start_date)
  4             AS quarter_number,
  5         start_date,
  6         ADD_MONTHS (start_date, 3) - 1
  7             AS end_date,
  8         (ADD_MONTHS (start_date, 3) - 1) - start_date
  9             AS number_of_days
 10    FROM (    SELECT ADD_MONTHS (ADD_MONTHS (TRUNC (CURRENT_DATE, 'Y') + 1, 1), 3 * (LEVEL - 5))    AS start_date
 11                FROM DUAL
 12          CONNECT BY LEVEL <= 12);

   FISCAL_YEAR    QUARTER_NUMBER    START_DATE     END_DATE    NUMBER_OF_DAYS
______________ _________________ _____________ ____________ _________________
FY2019                         1 02-FEB-19     01-MAY-19                   88
FY2019                         2 02-MAY-19     01-AUG-19                   91
FY2019                         3 02-AUG-19     01-NOV-19                   91
FY2019                         4 02-NOV-19     01-FEB-20                   91
FY2020                         1 02-FEB-20     01-MAY-20                   89
FY2020                         2 02-MAY-20     01-AUG-20                   91
FY2020                         3 02-AUG-20     01-NOV-20                   91
FY2020                         4 02-NOV-20     01-FEB-21                   91
FY2021                         1 02-FEB-21     01-MAY-21                   88
FY2021                         2 02-MAY-21     01-AUG-21                   91
FY2021                         3 02-AUG-21     01-NOV-21                   91
FY2021                         4 02-NOV-21     01-FEB-22                   91

12 rows selected.