我在FACT表中的值如下:
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
我如何填充17-Apr-17和31-May-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 :(得分: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
添加了Sep行以测试我的查询的真实性。
我的输出为:
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
我希望这是你正在寻找的。 p>