В базе данных oracle у меня есть данные, разделенные на «Центральная, Группа, ВРЕМЕННЫЙ НАБОР, ГОД, Квартал». Однако значения измерений, которые у меня были, существуют только для групп центров, что означает, что общие значения центров не зарегистрированы, и я должен их вычислить.
My idea was to use a subquery as the following:
WITH table1 as (SELECT
DMDO_ESTRUTURA_ORGANIZA_ET_V.CENTRAL_ABR AS NOME,
CASE
WHEN DMDO_ESTRUTURA_ORGANIZA_ET_V.CENTRAL_ABR <> 'NA'
THEN substr(DMDO_ESTRUTURA_ORGANIZA_ET_V.CENTRAL_ABR,-2)
ELSE 'NA'
END AS CENTRAL,
CASE
WHEN DMDO_ESTRUTURA_ORGANIZA_ET_V.GRUPO_ABR <> 'NA'
THEN 'G' || substr(DMDO_ESTRUTURA_ORGANIZA_ET_V.GRUPO_ABR,-1,1)
ELSE 'NA'
END AS GRUPO,
TRUNC(FOMNMN001."IMN_ANO_MES" , 'MM') AS TIMESTAMP,
EXTRACT(year from FOMNMN001."IMN_ANO_MES") AS YEAR,
CASE
WHEN EXTRACT(month from FOMNMN001."IMN_ANO_MES") = '1' OR EXTRACT(month from FOMNMN001."IMN_ANO_MES") = '2' OR EXTRACT(month from FOMNMN001."IMN_ANO_MES") = '3'
THEN '1'
WHEN EXTRACT(month from FOMNMN001."IMN_ANO_MES") = '4' OR EXTRACT(month from FOMNMN001."IMN_ANO_MES") = '5' OR EXTRACT(month from FOMNMN001."IMN_ANO_MES") = '6'
THEN '2'
WHEN EXTRACT(month from FOMNMN001."IMN_ANO_MES") = '7' OR EXTRACT(month from FOMNMN001."IMN_ANO_MES") = '8' OR EXTRACT(month from FOMNMN001."IMN_ANO_MES") = '9'
THEN '3'
WHEN EXTRACT(month from FOMNMN001."IMN_ANO_MES") = '10' OR EXTRACT(month from FOMNMN001."IMN_ANO_MES") = '11' OR EXTRACT(month from FOMNMN001."IMN_ANO_MES") = '12'
THEN '4'
END AS QUARTER,
SUM( FOMNMN001."IMN_TOTAL_OC") AS IMN_TOTAL_OC ,
SUM( FOMNMN001."IMN_TOTAL_OCNP") AS IMN_TOTAL_OCNP ,
SUM( FOMNMN001."IMN_T1I") AS IMN_T1I,
SUM( FOMNMN001."IMN_T17_MTBF") AS IMN_T17_MTBF,
SUM( FOMNMN001."IMN_T21_MTTF") AS IMN_T21_MTTF,
SUM( FOMNMN001."IMN_OP_PER_HORAS") AS IMN_OP_PER_HORAS,
SUM( FOMNMN001."IMN_IND_MAN_HORAS") AS IMN_IND_MAN_HORAS,
SUM( FOMNMN001."IMN_IND_NP_MAN_HORAS") AS IMN_IND_NP_MAN_HORAS
FROM
FOMNMN001 INNER JOIN DMDO_ESTRUTURA_ORGANIZA_ET_V
ON DMDO_ESTRUTURA_ORGANIZA_ET_V.HIERARQUIA=FOMNMN001.HIERARQUIA
AND DMDO_ESTRUTURA_ORGANIZA_ET_V.DIRECCAO IN ('2000PT')
AND DMDO_ESTRUTURA_ORGANIZA_ET_V.CENTRAL_ABR <> 'NA'
INNER JOIN DMDO_TEMPO
ON DMDO_TEMPO.DATA=FOMNMN001.DATA
AND DMDO_TEMPO.DATA >= '20190101'
AND MOD(TO_NUMBER (EXTRACT(month from FOMNMN001."IMN_ANO_MES")) , 3) = 0
GROUP BY DMDO_ESTRUTURA_ORGANIZA_ET_V.CENTRAL_ABR,
CASE
WHEN DMDO_ESTRUTURA_ORGANIZA_ET_V.CENTRAL_ABR <> 'NA'
THEN substr(DMDO_ESTRUTURA_ORGANIZA_ET_V.CENTRAL_ABR,-2)
ELSE 'NA'
END,
CASE
WHEN DMDO_ESTRUTURA_ORGANIZA_ET_V.GRUPO_ABR <> 'NA'
THEN 'G' || substr(DMDO_ESTRUTURA_ORGANIZA_ET_V.GRUPO_ABR,-1,1)
ELSE 'NA'
END,
TRUNC(FOMNMN001."IMN_ANO_MES" , 'MM'),
EXTRACT(year from FOMNMN001."IMN_ANO_MES"),
CASE
WHEN EXTRACT(month from FOMNMN001."IMN_ANO_MES") = '1' OR EXTRACT(month from FOMNMN001."IMN_ANO_MES") = '2' OR EXTRACT(month from FOMNMN001."IMN_ANO_MES") = '3'
THEN '1'
WHEN EXTRACT(month from FOMNMN001."IMN_ANO_MES") = '4' OR EXTRACT(month from FOMNMN001."IMN_ANO_MES") = '5' OR EXTRACT(month from FOMNMN001."IMN_ANO_MES") = '6'
THEN '2'
WHEN EXTRACT(month from FOMNMN001."IMN_ANO_MES") = '7' OR EXTRACT(month from FOMNMN001."IMN_ANO_MES") = '8' OR EXTRACT(month from FOMNMN001."IMN_ANO_MES") = '9'
THEN '3'
WHEN EXTRACT(month from FOMNMN001."IMN_ANO_MES") = '10' OR EXTRACT(month from FOMNMN001."IMN_ANO_MES") = '11' OR EXTRACT(month from FOMNMN001."IMN_ANO_MES") = '12'
THEN '4'
END
ORDER BY 4, 2, 3
)
Select NOME , CENTRAL , GRUPO , TIMESTAMP , YEAR , QUARTER ,
CASE
WHEN CENTRAL <> 'NA' AND GRUPO <> 'NA'
THEN IMN_TOTAL_OC
WHEN CENTRAL <> 'NA' AND GRUPO = 'NA'
THEN (SELECT SUM (IMN_TOTAL_OC) from table1 group by NOME , CENTRAL ,
TIMESTAMP , YEAR , QUARTER)
END AS IMN_OC
from table1
Expected Output (getting the values in red):
введите описание изображения здесь
Кто-нибудь знает, как это сделать?