使用子查询对列的值求和

Posted

技术标签:

【中文标题】使用子查询对列的值求和【英文标题】:Sum the values of a column using a subquery 【发布时间】:2020-07-14 16:57:08 【问题描述】:

我是一个 oracle 数据库,我将数据除以“Central、Group、TIMESTAMP、YEAR、Quarter”。但是,我只存在于中心组的测量值,这意味着中心的总值没有记录,我必须计算它们。

我的想法是使用如下子查询:

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 

预期输出(以红色显示值):

有人知道怎么做吗?

【问题讨论】:

请提及预期结果表 请不要使用图片提供代码。使用格式化为代码的纯文本。使用它是不可能的 我无法真正阅读查询,但我可以看到它比必要的复杂得多。 @salam abdeen。问问自己:如果有人以这种格式向您发送该问题,您会花时间回答吗?如果有趣的话回答问题,将屏幕截图转换为工作示例不是。如果你有一个最小的可重现测试用例会容易得多:一个表的 ddl(只有相关的列),几行数据的 dml,然后是你到目前为止尝试的 sql。这样,如果 SO 的任何成员想要重现您的示例,他们只需运行您的脚本即可复制您的设置。 【参考方案1】:

您可以使用 GROUP BY ROLLUP() 来计算小计和总计 - 请参阅 documentation 和 examples。假设我们有以下情况(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 

使用 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 

DBfiddle here.

另一件小事:在查看第二个屏幕截图时,我们看到您正在使用 CASE ... EXTRACT ... 来查找“季度数字”。这太复杂了。只需使用 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 ;

另请参阅:格式模型documentation。

【讨论】:

以上是关于使用子查询对列的值求和的主要内容,如果未能解决你的问题,请参考以下文章

根据来自不同列的 2 个其他值对列的值求和

SonataAdminBundle 如何在列表视图中对列的值求和

在子查询中为数据透视列的值实现 WHERE 子句?

如何聚合数据框并通过 r 中的重复行对列的值求和

SQL语句汇总——聚合函数分组子查询及组合查询

使用子查询对房间价格求和时出错