2017-08-14 76 views
0

我有一個事實表如下值:填充從季度日期的月日在甲骨文

As of Date F_Type  Value 
31-Mar-17 ABC Corp 1.0 
30-Jun-17 ABC Corp 1.1 

由於日期是季度結束日期,現在我需要編寫一個查詢得到的結果如下設置:(該值後的日期之間的月份結束日期被改變)

As of Date F_Type  Value 
31-Mar-17 ABC Corp 1.0 
30-Apr-17 ABC Corp 1.0 
31-May-17 ABC Corp 1.0 
30-Jun-17 ABC Corp 1.1 

我怎樣才能填充30-APR-17和31可能-17行?

代碼試圖至今:

SELECT 
As_Of_Date, 
F_Type, 
Value,X.* 
FROM FACT F INNER JOIN 
(SELECT M_DATE,trunc(M_DATE+1,'Q')-1 AS QTR_DATE FROm 
(Select TO_DATE('31-JAN-2017','DD-MON-YYYY') AS M_DATE from dual UNION ALL 
Select TO_DATE('28-FEB-2017','DD-MON-YYYY') from dual UNION ALL 
Select TO_DATE('31-MAR-2017','DD-MON-YYYY') from dual UNION ALL 
Select TO_DATE('30-APR-2017','DD-MON-YYYY') from dual UNION ALL 
Select TO_DATE('31-MAY-2017','DD-MON-YYYY') from dual UNION ALL 
Select TO_DATE('30-JUN-2017','DD-MON-YYYY') from dual UNION ALL 
Select TO_DATE('31-JUL-2017','DD-MON-YYYY') from dual UNION ALL 
Select TO_DATE('31-AUG-2017','DD-MON-YYYY') from dual UNION ALL 
Select TO_DATE('30-SEP-2017','DD-MON-YYYY') from dual UNION ALL 
Select TO_DATE('31-OCT-2017','DD-MON-YYYY') from dual UNION ALL 
Select TO_DATE('30-NOV-2017','DD-MON-YYYY') from dual UNION ALL 
Select TO_DATE('31-DEC-2017','DD-MON-YYYY') from dual))X 
ON F.As_Of_Date=X.QTR_DATE 

這給了我類似下面:

As of Date F_Type  Value M_Date Qtr_Date 
3/31/2017 ABC Corp 1.0 3/31/2017 3/31/2017 
3/31/2017 ABC Corp 1.0 4/30/2017 3/31/2017 
3/31/2017 ABC Corp 1.0 5/31/2017 3/31/2017 
6/30/2017 ABC Corp 1.1 6/30/2017 6/30/2017 
6/30/2017 ABC Corp 1.1 7/31/2017 6/30/2017 
6/30/2017 ABC Corp 1.1 8/31/2017 6/30/2017 

有沒有辦法來調整這個查詢和輸出,以及。

回答

0

我認爲下面的查詢可能適合你,我試着用你的表格檢查輸出。

SELECT * FROM FACT 
UNION 
SELECT ADD_MONTHS(AS_OF_DATE,LVL.LVL2) DT, 
      FACT.F_TYPE, 
      FACT.VALUE 
FROM FACT, 
     (SELECT LEVEL AS LVL2 
     FROM DUAL 
      CONNECT BY LEVEL<=2) LVL 
     ,(SELECT MAX(AS_OF_DATE) DT2 
      FROM FACT) FC2 
     WHERE ADD_MONTHS(AS_OF_DATE,LVL.LVL2)<=FC2.DT2 

我已經採取了以下數據集:

As of Date F_Type  Value 
31-Mar-17 ABC Corp 1.0 
30-Jun-17 ABC Corp 1.1 
30-Sep-17 ABC Corp 1.2 

添加了九月一行來測試我的查詢的真實性。

和我得到的輸出爲:

As of Date F_Type  Value 
31-Mar-17 ABC Corp 1.0 
30-Apr-17 ABC Corp 1.0 
31-May-17 ABC Corp 1.0 
30-Jun-17 ABC Corp 1.1 
31-Jul-17 ABC Corp 1.1 
30-Aug-17 ABC Corp 1.1 
30-Sep-17 ABC Corp 1.2 

我希望這是你在找什麼。

+0

可能你應該使用'last_day()'來確保生成的日期是月末,因爲30rh不是7月的最後一天。 – APC

+0

我沒有粘貼來自我的數據庫的結果數據集,也許我手工編輯了它,我將更正答案。謝謝@APC讓我知道。假設數據將在月末結束,所以我使用了add_months,這使得下一個日期也是月份結束。 – kratostoical

+0

謝謝@kratostoical –