Reputation: 67
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
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
Upvotes: 0
Views: 70
Reputation: 2906
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 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.
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. And, the LQ will count the same things as all of the fields. 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.
Upvotes: 0
Reputation: 1667
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
Upvotes: 0
Reputation: 10701
You may use a simple query like this to obtain the result for LQR%
predicate that you request in your question
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%'
Upvotes: 0
Reputation: 95
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)
Upvotes: 1