SQL中同一列的值总和

时间:2019-01-10 12:36:20

标签: sql oracle aggregate-functions

我需要显示一列的总和,例如: Result of the selects

例如,如何创建“ LQ”的总和: 0+0+38+1010+216+664

我的代码:

select pla.DSC_ACO,
       sum(case when res.cod_ordem_producao like 'LQX%' then 1 else 0 end) as lqx,
       sum(case when res.cod_ordem_producao like 'LQP%' then 1 else 0 end) as lqp,
       sum(case when res.cod_ordem_producao like 'LQT%' then 1 else 0 end) as LQT,
       sum(case when res.cod_ordem_producao like 'LQRT%' then 1 else 0 end) as lqrt,
       sum(case when res.cod_ordem_producao like 'LQRZ%' then 1 else 0 end) as lqrz,
       sum(case when res.cod_ordem_producao like 'LQZ%' then 1 else 0 end) as LQZ,
       sum(case when res.cod_ordem_producao like 'LQRW%' then 1 else 0 end) as lqrw,
       sum(case when res.cod_ordem_producao like 'LQW%' then 1 else 0 end) as LQW,
       sum(case when res.cod_ordem_producao like 'LQ%' then 1 else 0 end) as Lq,
       sum(case when res.cod_ordem_producao like 'LQR%' then 1 else 0 end) as LQR
from QT_QTS.RES_TUBO_REVENIMENTO2 res, QT_QTS.PLA_ORDEM_PRODUCAO pla
where res.COD_ORDEM_PRODUCAO = pla.COD_ORDEM_PRODUCAO
and res.DTH_CRIACAO_REG >= :dthini 
and res.DTH_CRIACAO_REG <=:dthfim
group by pla.DSC_ACO

4 个答案:

答案 0 :(得分:1)

我希望它能起作用:)

select pla.DSC_ACO,
       sum(case when res.cod_ordem_producao like 'LQX%' then 1 else 0 end) as lqx,
       sum(case when res.cod_ordem_producao like 'LQP%' then 1 else 0 end) as lqp,
       sum(case when res.cod_ordem_producao like 'LQT%' then 1 else 0 end) as LQT,
       sum(case when res.cod_ordem_producao like 'LQRT%' then 1 else 0 end) as lqrt,
       sum(case when res.cod_ordem_producao like 'LQRZ%' then 1 else 0 end) as lqrz,
       sum(case when res.cod_ordem_producao like 'LQZ%' then 1 else 0 end) as LQZ,
       sum(case when res.cod_ordem_producao like 'LQRW%' then 1 else 0 end) as lqrw,
       sum(case when res.cod_ordem_producao like 'LQW%' then 1 else 0 end) as LQW,
       sum(case when res.cod_ordem_producao like 'LQ%' then 1 else 0 end) as Lq,
       sum(case when res.cod_ordem_producao like 'LQR%' then 1 else 0 end) as LQR
from QT_QTS.RES_TUBO_REVENIMENTO2 res, QT_QTS.PLA_ORDEM_PRODUCAO pla
where res.COD_ORDEM_PRODUCAO = pla.COD_ORDEM_PRODUCAO
and res.DTH_CRIACAO_REG >= :dthini 
and res.DTH_CRIACAO_REG <=:dthfim
group by pla.DSC_ACO
UNION ALL
SELECT 
'TOTAL' DSC,
SUM(lqx),
SUM(lqp),
SUM(LQT),
SUM(lqrt),
SUM(qrz),
SUM(LQZ),
SUM(lqrw),
SUM(LQW),
SUM(Lq),
SUM(LQR)
FROM (select pla.DSC_ACO,
       sum(case when res.cod_ordem_producao like 'LQX%' then 1 else 0 end) as lqx,
       sum(case when res.cod_ordem_producao like 'LQP%' then 1 else 0 end) as lqp,
       sum(case when res.cod_ordem_producao like 'LQT%' then 1 else 0 end) as LQT,
       sum(case when res.cod_ordem_producao like 'LQRT%' then 1 else 0 end) as lqrt,
       sum(case when res.cod_ordem_producao like 'LQRZ%' then 1 else 0 end) as lqrz,
       sum(case when res.cod_ordem_producao like 'LQZ%' then 1 else 0 end) as LQZ,
       sum(case when res.cod_ordem_producao like 'LQRW%' then 1 else 0 end) as lqrw,
       sum(case when res.cod_ordem_producao like 'LQW%' then 1 else 0 end) as LQW,
       sum(case when res.cod_ordem_producao like 'LQ%' then 1 else 0 end) as Lq,
       sum(case when res.cod_ordem_producao like 'LQR%' then 1 else 0 end) as LQR
from QT_QTS.RES_TUBO_REVENIMENTO2 res, QT_QTS.PLA_ORDEM_PRODUCAO pla
where res.COD_ORDEM_PRODUCAO = pla.COD_ORDEM_PRODUCAO
and res.DTH_CRIACAO_REG >= :dthini 
and res.DTH_CRIACAO_REG <=:dthfim
group by pla.DSC_ACO)

答案 1 :(得分:0)

您可以使用类似这样的简单查询来获取您在问题中要求的LQR%谓词的结果

select count(*) as LQR
from QT_QTS.RES_TUBO_REVENIMENTO2 res
join QT_QTS.PLA_ORDEM_PRODUCAO pla on res.COD_ORDEM_PRODUCAO = pla.COD_ORDEM_PRODUCAO
where res.DTH_CRIACAO_REG >= :dthini 
      and res.DTH_CRIACAO_REG <=:dthfim
      and res.cod_ordem_producao like 'LQR%'

答案 2 :(得分:0)

select sum(lqx), sum(lqp), sum(lqt), sum(lqrt), sum(lqrz), sum(lqz), sum(lqrw), 
sum(lqw), sum(lq)
, sum(lqr) from
(select pla.DSC_ACO,
   sum(case when res.cod_ordem_producao like 'LQX%' then 1 else 0 end) as lqx,
   sum(case when res.cod_ordem_producao like 'LQP%' then 1 else 0 end) as lqp,
   sum(case when res.cod_ordem_producao like 'LQT%' then 1 else 0 end) as LQT,
   sum(case when res.cod_ordem_producao like 'LQRT%' then 1 else 0 end) as lqrt,
   sum(case when res.cod_ordem_producao like 'LQRZ%' then 1 else 0 end) as lqrz,
   sum(case when res.cod_ordem_producao like 'LQZ%' then 1 else 0 end) as LQZ,
   sum(case when res.cod_ordem_producao like 'LQRW%' then 1 else 0 end) as lqrw,
   sum(case when res.cod_ordem_producao like 'LQW%' then 1 else 0 end) as LQW,
   sum(case when res.cod_ordem_producao like 'LQ%' then 1 else 0 end) as Lq,
   sum(case when res.cod_ordem_producao like 'LQR%' then 1 else 0 end) as LQR
from QT_QTS.RES_TUBO_REVENIMENTO2 res, QT_QTS.PLA_ORDEM_PRODUCAO pla
where res.COD_ORDEM_PRODUCAO = pla.COD_ORDEM_PRODUCAO
and res.DTH_CRIACAO_REG >= :dthini 
and res.DTH_CRIACAO_REG <=:dthfim
group by pla.DSC_ACO) as values

答案 3 :(得分:0)

使用通用表表达式来组织所需的数据,然后只需简单地从中进行选择即可。 WITH子句中的CTE允许您从普通表中进行选择。

WITH temp_table AS (
    select pla.DSC_ACO,
           case when res.cod_ordem_producao like 'LQX%' then 1 else 0 end as LQX,
           case when res.cod_ordem_producao like 'LQP%' then 1 else 0 end as LQP,
           case when res.cod_ordem_producao like 'LQT%' then 1 else 0 end as LQT,
           case when res.cod_ordem_producao like 'LQRT%' then 1 else 0 end as LQRT,
           case when res.cod_ordem_producao like 'LQRZ%' then 1 else 0 end as LQRZ,
           case when res.cod_ordem_producao like 'LQZ%' then 1 else 0 end as LQZ,
           case when res.cod_ordem_producao like 'LQRW%' then 1 else 0 end as LQRW,
           case when res.cod_ordem_producao like 'LQW%' then 1 else 0 end as LQW,
           case when res.cod_ordem_producao like 'LQR%' then 1 else 0 end as LQR,
           case when res.cod_ordem_producao like 'LQ%' then 1 else 0 end as LQ
    from QT_QTS.RES_TUBO_REVENIMENTO2 res, QT_QTS.PLA_ORDEM_PRODUCAO pla
    where res.COD_ORDEM_PRODUCAO = pla.COD_ORDEM_PRODUCAO
        and res.DTH_CRIACAO_REG >= :dthini 
        and res.DTH_CRIACAO_REG <= :dthfim)
SELECT SUM(LQ) AS LQ_SUM FROM temp_table

您没有指定查询所需的最终格式,但您的问题仅询问如何获取该列的总和。如果您需要扩展逻辑以包括其他聚合,则应该能够看到如何在此处的最终SELECT语句中添加更多字段。

此外,您应该注意,您将要重复计算一些数据。例如,LQR也将计数与LQRT,LQRZ和LQRW中相同的值。并且,LQ将计数与所有字段相同的事物。这可能是您想要的行为,但是如果您希望每个值都具有唯一的bin,则需要首先对其进行更多的逻辑处理。