从 Oracle 中的季度日期填充每月日期
Populate the monthly date from quarterly date in Oracle
我在 FACT table 中的值如下:
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
如何填充 2017 年 4 月 30 日和 17 年 5 月 31 日的行?
到目前为止尝试过的代码:
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
有没有办法调整这个查询和输出。
我认为以下查询可能适合您,我尝试了您的 table 并检查了输出。
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
希望这就是您要找的。
我在 FACT table 中的值如下:
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
如何填充 2017 年 4 月 30 日和 17 年 5 月 31 日的行?
到目前为止尝试过的代码:
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
有没有办法调整这个查询和输出。
我认为以下查询可能适合您,我尝试了您的 table 并检查了输出。
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
希望这就是您要找的。