[英]Sum the values of a column using a subquery
I an oracle database, I have the data divided by "Central, Group, TIMESTAMP, YEAR, Quarter".我是一个 oracle 数据库,我将数据除以“Central、Group、TIMESTAMP、YEAR、Quarter”。 However, the values of the measurements that I have only existed for the groups of the centers, which means the total values of the centers are not registered, and I have to calculate them.但是,我只存在于中心组的测量值,这意味着中心的总值没有注册,我必须计算它们。
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):预期 Output(以红色显示值):
Does anyone have an idea of how to do this?有谁知道如何做到这一点?
You could use GROUP BY ROLLUP() for calculating subtotals and a grand total - see documentation and examples .您可以使用 GROUP BY ROLLUP() 来计算小计和总计 - 请参阅文档和示例。 Suppose we have the following situation (ORIGINAL being a table name or a view name):假设我们有以下情况(ORIGINAL 是表名或视图名):
select
nome, central, grupo, trunc( tstamp )
, yr, quarter, totaloc, totalocnp
from original ;
NOME CENTRAL GRUPO TRUNC(TSTAMP) YR QUARTER TOTALOC TOTALOCNP
_______ __________ ________ ________________ _______ __________ __________ ____________
PTLR LR G1 15-JUL-20 2019 1 3 1
PTLR LR G2 15-JUL-20 2019 1 1
PTLR LR NA 15-JUL-20 2019 1
PTLJ RJ G1 15-JUL-20 2019 1 2 1
PTLJ RJ G2 15-JUL-20 2019 1 1
PTLJ RJ G3 15-JUL-20 2019 1 4 3
PTLJ RJ NA 15-JUL-20 2019 1
PTLN SN G1 15-JUL-20 2019 1 5 5
PTLN SN G2 15-JUL-20 2019 1 4 3
PTLN SN G3 15-JUL-20 2019 1 2 1
PTLN SN G4 15-JUL-20 2019 1 3 3
PTLN SN NA 15-JUL-20 2019 1
PTLR LR G1 13-SEP-20 2019 2 3 3
PTLR LR G2 13-SEP-20 2019 2 1 1
PTLR LR NA 13-SEP-20 2019 2
PTLJ RJ G1 13-SEP-20 2019 2 1 0
PTLJ RJ G2 13-SEP-20 2019 2 0 0
PTLJ RJ G3 13-SEP-20 2019 2 4 3
Query with GROUP BY ROLLUP使用 GROUP BY ROLLUP 查询
select
nome, central, yr, quarter
, sum( totaloc ) totaloc
, sum( totalocnp ) totalocnp
from original
group by rollup( ( nome, central ), yr, quarter )
order by 1, 2, 3, 4
;
-- result
NOME CENTRAL YR QUARTER TOTALOC TOTALOCNP
_______ __________ _______ __________ __________ ____________
PTLJ RJ 2019 1 7 4
PTLJ RJ 2019 2 5 3
PTLJ RJ 2019 12 7
PTLJ RJ 12 7
PTLN SN 2019 1 14 12
PTLN SN 2019 14 12
PTLN SN 14 12
PTLR LR 2019 1 4 1
PTLR LR 2019 2 4 4
PTLR LR 2019 8 5
PTLR LR 8 5
34 24
Another little thing: when looking at your second screen shot, we see that you are using CASE... EXTRACT... for finding the "quarter numbers".另一件小事:在查看您的第二个屏幕截图时,我们看到您正在使用 CASE... EXTRACT... 来查找“季度数字”。 This is too complicated.这太复杂了。 Just use TO_CHAR( thedate, 'Q').只需使用 TO_CHAR(thedate, 'Q')。
-- eg
select to_char( systimestamp, 'Q' ) from dual ;
TO_CHAR(SYSTIMESTAMP,'Q')
____________________________
3
-- with your "original" table:
select to_char( trunc( tstamp ), 'Q' ) from original ;
See also: format models documentation .另请参阅:格式化模型文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.