Only positive numbers in SUM aggregation

Asked

Viewed 106 times

5

In this consultation, in ,SUM(quota_re_mes1) AS NO_MES i would like it to add up only the positive numbers, is there any way to do that?

    SELECT orcdotac.conta_desp 
       ,orcplade.descricao 
       ,orcplade.complemento 
       ,SUM(orcdotac.sld_orc_ano) AS CREDITOS_ORCAMENTARIOS 
       ,(SUM(orcdotac.SLD_ORC_ANO) + SUM(orcdotac.SLD_SUPL_ANO) + SUM(orcdotac.SLD_ESP_ANO) + SUM(orcdotac.SLD_EXT_ANO)) - SUM(orcdotac.SLD_RE_ANO) AS SALDO_ORCAMENTARIO 
       ,SUM(quota_re_mes1) AS NO_MES 
       ,(SUM(orcdotac.SLD_EMP_EMPEN) - SUM(orcdotac.SLD_EMP_ANULADO)) AS NO_ANO 
       ,(SUM(orcdotac.SLD_ORC_ANO) + SUM(orcdotac.SLD_SUPL_ANO) + SUM(orcdotac.SLD_ESP_ANO) + SUM(orcdotac.SLD_EXT_ANO)) - SUM(orcdotac.SLD_RE_ANO) - (SUM(orcdotac.SLD_EMP_EMPEN) - SUM(orcdotac.SLD_EMP_ANULADO)) AS DIFERENCAS 
FROM orcdotac 
LEFT JOIN orcplade ON 
        (orcdotac.conta_desp = orcplade.conta_desp) 
WHERE orcdotac.conta_desp IS NOT NULL and codigo_tipo = 1  
GROUP BY orcdotac.conta_desp, orcdotac.sub_item, orcplade.descricao, orcplade.complemento
ORDER BY conta_desp

1 answer

4


....
, SUM(CASE WHEN quota_re_mes1 > 0 THEN quota_re_mes1 ELSE 0 END) AS NO_MES
....
  • 1

    wow, so easy... but I couldn’t find anything like this on google hahaha

Browser other questions tagged

You are not signed in. Login or sign up in order to post.