2010-04-20 91 views
10

以下數據庫視圖截斷日至本財年(4月1日):截斷日期財政年度

CREATE OR REPLACE VIEW FISCAL_YEAR_VW AS 
SELECT 
    CASE 
    WHEN to_number(to_char(SYSDATE, 'MM')) < 4 THEN  
     to_date('1-APR-'||to_char(add_months(SYSDATE, -12), 'YYYY'), 'dd-MON-yyyy') 
    ELSE 
     to_date('1-APR-'||to_char(SYSDATE, 'YYYY'), 'dd-MON-yyyy') 
    END AS fiscal_year 
FROM 
    dual; 

這使我們能夠基於今天的日期計算當前會計年度。

如何簡化或優化此計算?

回答

10
ADD_MONTHS(TRUNC(ADD_MONTHS(SYSDATE,-3),'YYYY'),3) 
+0

輝煌。謝謝。 – 2010-04-21 15:56:26

0

或許這...

SELECT to_date('01/04/' || 
to_char(extract(YEAR FROM SYSDATE) 
- CASE WHEN extract(MONTH FROM SYSDATE) BETWEEN 1 AND 4 THEN 1 ELSE 0 END), 
'DD/MM/YYYY') FROM dual; 

我想這是另一種選擇......

SELECT add_months(trunc(SYSDATE) - extract(DAY FROM SYSDATE) + 1, 
- (extract(MONTH FROM SYSDATE) + CASE 
WHEN extract(MONTH FROM SYSDATE) <= 4 THEN 12 ELSE 0 END) + 4) 
FROM dual; 

其他選項作爲重寫返回日期的功能,或邏輯可以簡化如果您只能返回當前會計年度的年數,因爲您只需要to_char中的邏輯。

+0

查詢結果不同。你現在提供的代碼返回2009-04-01。它應該返回2010-04-01。 – 2010-04-20 22:56:07

0

TRUNC()可以有效地應用於具有不同格式掩碼的日期。最有針對性的是,trunc(sysdate, 'yyyy')給了我們今年的第一天。因此,這會給我們當年的01-APR ...

add_months(trunc(sysdate, 'yyyy'), 3) 

,這是日期前一年...

add_months(trunc(add_months(sysdate, -12), 'yyyy'), 3) 

所以:

CREATE OR REPLACE VIEW FISCAL_YEAR_VW AS 
WITH cte as 
    (select add_months(trunc(sysdate, 'yyyy'), 3) as this_year 
      , add_months(trunc(add_months(sysdate, -12), 'yyyy'), 3) as last_year 
     from dual) 
SELECT 
    CASE 
    WHEN SYSDATE >= cte.this_year THEN  
     cte.this_year 
    ELSE 
     cte.last_year 
    END AS fiscal_year 
FROM 
    cte; 

警告:我還沒有機會測試此代碼,因此它可能包含拼寫錯誤。我會稍後測試它,並在必要時進行更正。

0

我發現oracle的TO_CHAR(date,'Q')特性對於計算會計日曆非常有用。下面的查詢使用「與」子句建立兩件事

  1. 樣本數據 - test_dates表。
  2. fiscal_map - 將日曆季度簡單映射到財政日曆。在這個例子中,第4個日曆季度是第1個財政季度(10月1日)。

實施例:

with test_dates as (
select sysdate + level * 80 test_date from dual connect by level < 11 
), 
fiscal_map as (
select 1 cal, 2 fiscal from dual 
union 
select 2 cal, 3 fiscal from dual 
union 
select 3 cal, 4 fiscal from dual 
union 
select 4 cal, 1 fiscal from dual 
) 
select 
test_date, 
TO_CHAR(test_date, 'Q') cal_quarter, 
fiscal_map.fiscal, 
(case when CAL < fiscal then 
    TO_CHAR(test_date, 'yyyy') + 0 
    else TO_CHAR(test_date, 'yyyy') + 1 
end) FISCAL_YEAR 
from test_dates, fiscal_map 
where fiscal_map.cal = TO_CHAR(test_date, 'Q') 
order by test_date 

輸出:

TEST_DT CAL_Q FISCAL Q FISCAL_YR 
22-Jul-10 3 4 2010 
10-Oct-10 4 1 2011 
29-Dec-10 4 1 2011 
19-Mar-11 1 2 2011 
07-Jun-11 2 3 2011 
26-Aug-11 3 4 2011 
14-Nov-11 4 1 2012 
02-Feb-12 1 2 2012 
22-Apr-12 2 3 2012 
11-Jul-12 3 4 2012 
0
select T.USERNAME,T.CREATED, 

CASE WHEN EXTRACT (MONTH FROM T.CREATED)>=4 AND EXTRACT (MONTH FROM T.CREATED)<=12 THEN 
TO_CHAR(EXTRACT (YEAR FROM T.CREATED))||'-'||TO_CHAR(EXTRACT (YEAR FROM T.CREATED)+1) 
    WHEN EXTRACT (MONTH FROM T.CREATED)<4 THEN 
TO_CHAR(EXTRACT (YEAR FROM T.CREATED)-1)||'-'||TO_CHAR(EXTRACT (YEAR FROM T.CREATED)) ELSE NULL END FY 
from sys.dba_users t WHERE T.USERNAME in ('101655','100149') 

輸出將是:

1 101655 14/01/2014 12:21:53 2013-2014 
2 100149 05/05/2012 16:55:00 2012-2013 
相關問題