0
I have 2 subselects, in SQL, that bring 2 different information, with the sum data. How to add the 2 "sums" and bring in a new column, or a single column?
CASE
WHEN sd3.D3_UM = 'PC' THEN
(SELECT ISNULL(sum(sd3_sub1.D3_QTSEGUM),0) AS D3_QUANT_pc
FROM SD3010 AS sd3_sub1 WITH(NOLOCK)
WHERE sd3_sub1.D3_TM = '010'
AND sd3_sub1.D3_LOCAL IN ('01',
'02',
'98')
AND sd3_sub1.D3_EMISSAO BETWEEN (LEFT(sd3.D3_EMISSAO,6) + '01') AND (LEFT(sd3.D3_EMISSAO,6) + '31')
AND sd3_sub1.D3_FILIAL = sd3.D3_FILIAL
AND sd3_sub1.D3_CC LIKE LEFT(sd3.D3_CC,5) + '%'
AND sd3_sub1.D3_UM = 'PC'
AND sd3_sub1.D_E_L_E_T_ <> '*'
GROUP BY sd3_sub1.D3_UM)
ELSE
(SELECT ISNULL(sum(sd3_sub1.D3_QUANT),0) AS D3_QUANT_not_pc
FROM SD3010 AS sd3_sub1 WITH(NOLOCK)
WHERE sd3_sub1.D3_TM = '010'
AND sd3_sub1.D3_LOCAL IN ('01',
'02',
'98')
AND sd3_sub1.D3_EMISSAO BETWEEN (LEFT(sd3.D3_EMISSAO,6) + '01') AND (LEFT(sd3.D3_EMISSAO,6) + '31')
AND sd3_sub1.D3_FILIAL = sd3.D3_FILIAL
AND sd3_sub1.D3_CC LIKE LEFT(sd3.D3_CC,5) + '%'
AND sd3_sub1.D3_UM <> 'PC'
AND sd3_sub1.D_E_L_E_T_ <> '*'
GROUP BY sd3_sub1.D3_UM)
END producao
Make the result a subquery, or use
with
transforming the result into a common table:with resultado as ( /* Seu select */) select D3_QUANT_pc+D3_QUANT_not_pc as Soma
– Ricardo Pontual
Sqlserver or mysql? WITH not supported in mysql.
– Ismael
@Thiagoalessandro: I notice that you are using what was proposed in http://answall.com/questions/185921/como-utilizar-o-like-em-uma-compara%C3%A7%C3%a3o-de-campos-em-tabelas-diferentes
– José Diz