尝试从物化视图中的另一列计算 SUM

Trying to calculate a SUM from another column in Materialized View

我正在尝试计算 Oracle MV 中每个月的工作日总和

这是我的要求:

CREATE MATERIALIZED VIEW DIM_DATE_MV
BUILD IMMEDIATE
REFRESH COMPLETE ON DEMAND
START WITH sysdate NEXT (TRUNC(sysdate)+1)  + 7 / 24
as SELECT
CAL.DATE_D as ID_DATE,
(CASE WHEN (
    (TRIM(TO_CHAR(CAL.DATE_D,'Day','nls_date_language=english')) IN ('Saturday','Sunday')) OR 
    (TRIM(TO_CHAR(CAL.DATE_D,'DD-MM')) IN ('01-01', '01-05', '08-05', '14-07', '15-08', '01-11', '11-11', '25-12')) OR 
    (TO_CHAR(CAL.DATE_D, 'DD-MM-YYYY') IN  (SELECT TO_CHAR(DOFF.DATE_OFF, 'DD-MM-YYYY') FROM ODSISIC.DAY_OFF DOFF where DOFF.IMPACT='ALL'))
           )  THEN 0 ELSE 1 END) as IS_WORKING_DAY,


(CASE WHEN TO_CHAR(CAL.DATE_D , 'YYYY-MM') = TO_CHAR(CAL.DATE_D , 'YYYY-MM') THEN (Select SUM(IS_WORKING_DAY) from DIM_DATE_MV group by CAL.YEAR_MONTH_NUM) ELSE 0 END)
 as NB_WORKING_DAY_MONTH

FROM ODSISIC.ORACLE_CALENDAR CAL
LEFT JOIN ODSISIC.DAY_OFF DOFF
ON DOFF.DATE_OFF = CAL.DATE_D

IS_WORKING_DAY = 0 如果它是 table DATE_OFF 中的假期、周末或日期,其中包含每年不同日期的所有假期。

我想要 IS_WORKING_DAY = 1 in NB_WORKING_DAY_MONTH.

月份的 SUM GROUP BY

如何直接在我的查询中计算此 SUM,而不是为我与 DAY_OFF table 的连接创建一个中间 table?

谢谢:)

经过明智的思考,我通过重做 SQL 查询解决了问题:

CREATE MATERIALIZED VIEW DIM_DATE_MV
BUILD IMMEDIATE
REFRESH COMPLETE ON DEMAND
START WITH sysdate NEXT (TRUNC(sysdate)+1)  + 7 / 24
as SELECT
CAL.DATE_D as ID_DATE,
IS_WORKING_DAY as IS_WORKING_DAY,

A.SUM as NB_WORKING_DAY_MONTH

FROM  (SELECT SUM(IS_WORKING_DAY) as SUM, OCAL.YEAR_MONTH_NUM as ID_MONTH from ODSISIC.ORACLE_CALENDAR OCAL group by OCAL.YEAR_MONTH_NUM) A
INNER JOIN ODSISIC.ORACLE_CALENDAR CAL 
on CAL.YEAR_MONTH_NUM = A.ID_MONTH
LEFT JOIN ODSISIC.DAY_OFF DOFF
ON DOFF.DATE_OFF = CAL.DATE_D
;

我在创建视图之前计算了工作日(这意味着我的 table DATE_OFF 必须在 ORACLE_CALENDAR 之前喂食)

我根据 id_month 添加了一个连接来填充我的 table。

现在工作正常