How to make a Select to not show reset result?

Asked

Viewed 614 times

2

I have the select below, which always brings me two lines, one with zero and the other with value:

SELECT COUNT(DISTINCT ROMANEIO) ROMANEIO FROM PCN_ROMANEIO_DISTR_ITEM WHERE USUARIO = 'junior'
UNION ALL
SELECT COUNT(DISTINCT ROMANEIO) ROMANEIO FROM PCN_ROMANEIO_DISTR_ITEM WHERE USUARIO_EMPILHADEIRA = 'junior'

The result is below:

inserir a descrição da imagem aqui

What you would need is to make it only display the result with a value greater than 0. Only one will have value, always. Any suggestion?

2 answers

4


Use a Subselect and filter the result of select internor.

select * from 
(
    SELECT COUNT(DISTINCT ROMANEIO) ROMANEIO FROM PCN_ROMANEIO_DISTR_ITEM WHERE USUARIO = 'junior'
    UNION ALL
    SELECT COUNT(DISTINCT ROMANEIO) ROMANEIO FROM PCN_ROMANEIO_DISTR_ITEM WHERE USUARIO_EMPILHADEIRA = 'junior'
)saida
where saida.ROMANEIO > 0
  • 1

    Simple and efficient. Thank you very much!

2

You can use the HAVING clause to filter aggregate values:

SELECT COUNT(DISTINCT ROMANEIO) ROMANEIO 
  FROM PCN_ROMANEIO_DISTR_ITEM 
 WHERE USUARIO = 'junior'
HAVING COUNT(DISTINCT ROMANEIO) > 0
UNION ALL
SELECT COUNT(DISTINCT ROMANEIO) ROMANEIO 
  FROM PCN_ROMANEIO_DISTR_ITEM 
 WHERE USUARIO_EMPILHADEIRA = 'junior 
HAVING COUNT(DISTINCT ROMANEIO) > 0;

Browser other questions tagged

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