在oracle SQL

时间:2018-09-14 09:03:53

标签: sql oracle fiscal dayofmonth

查询应返回当前会计月的天数。请检查以下查询:

SELECT *
FROM GL_PERIODS
WHERE PERIOD_SET_NAME='Fiscal Year'
AND period_year =(SELECT DISTINCT period_year
                                FROM gl_periods
                               WHERE     TRUNC (SYSDATE) BETWEEN start_date
                                                             AND end_date)

对于期间名称:SEP-19,开始日期为2018年8月26日。因此,sysdate的天数应为16。

样本数据:

period_set_name    period_name    start_date    end_date   entered_period_name
Fiscal Year         AUG-19        7/29/2018    8/25/2018    AUG
Fiscal Year         SEP-19        8/26/2018    9/22/2018    SEP
Fiscal Year         OCT-19        9/23/2018    10/27/2018   OCT

预期输出:16

1 个答案:

答案 0 :(得分:0)

Oracle设置

CREATE TABLE GL_PERIODS(
  period_set_name VARCHAR2(20),
  start_date          DATE,
  end_date            DATE,
  period_name         CHAR(6) GENERATED ALWAYS AS ( CAST( TO_CHAR( end_date, 'MON-YY' ) AS CHAR(6) ) ) VIRTUAL,
  entered_period_name CHAR(3) GENERATED ALWAYS AS ( CAST( TO_CHAR( end_date, 'MON' ) AS CHAR(3) ) ) VIRTUAL,
  period_year         NUMBER(4) GENERATED ALWAYS AS ( EXTRACT( YEAR FROM end_date ) ) VIRTUAL
);

INSERT INTO GL_PERIODS( period_set_name, start_date, end_date )
  SELECT 'Fiscal Year', DATE '2018-07-29', DATE '2018-08-25' FROM DUAL UNION ALL
  SELECT 'Fiscal Year', DATE '2018-08-26', DATE '2018-09-22' FROM DUAL UNION ALL
  SELECT 'Fiscal Year', DATE '2018-09-23', DATE '2018-10-27' FROM DUAL;

查询

SELECT TRUNC( SYSDATE - start_date + 1 ) AS fiscal_day_no
FROM   GL_PERIODS
WHERE  start_date <= SYSDATE
AND    SYSDATE < END_DATE + INTERVAL '1' DAY;

输出

| FISCAL_DAY_NO |
|---------------|
|            20 |