简体   繁体   English

SQL中同一列的值总和

[英]Sum of Values ​of the Same Column in SQL

I need to show the sum of a column, like: 我需要显示一列的总和,例如: 选择结果

How can I create a sum of the "LQ's", for exemple: 0+0+38+1010+216+664 我如何创建“ LQ”的和,例如: 0+0+38+1010+216+664

My code: 我的代码:

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

I hope it works :) 我希望它能起作用:)

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)

You may use a simple query like this to obtain the result for LQR% predicate that you request in your question 您可以使用类似这样的简单查询来获取您在问题中请求的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%'
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

Use a Common Table Expression to organize the data you want, then just select from it with a simple summation. 使用通用表表达式来组织所需的数据,然后通过简单的汇总从中选择。 The CTE in the WITH clause will allow you to select from that just like it were a normal table. 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

You didn't specify the final format you needed the query in, but your question only asked how do you get the sum of that column. 您没有指定查询所使用的最终格式,但是您的问题仅询问如何获取该列的总和。 If you need to expand the logic to include other aggregations, you should be able to see how to add more fields to the final SELECT statement there. 如果您需要扩展逻辑以包括其他聚合,则应该能够看到如何向此处的最终SELECT语句添加更多字段。

Also, you should note that you are going to be double counting some data. 另外,您应该注意,您将要重复计算一些数据。 For example, LQR will also be counting the same values as in LQRT, LQRZ, and LQRW. 例如,LQR也将计数与LQRT,LQRZ和LQRW中相同的值。 And, the LQ will count the same things as all of the fields. 并且,LQ将计数与所有字段相同的事物。 This might be the behavior you want, but if you want a unique bin for each value to fall in, you are going to need to do some more logic manipulation to it first. 这可能是您想要的行为,但是如果您希望每个值都具有唯一的bin,则需要首先对其进行更多的逻辑处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM