How to make a Count in Mysql using case or if?

Asked

Viewed 455 times

1

I need to know how I can do a search in mysql to know how many documents I have to analyze with a condition that if the previous process is not released it cannot count.

SELECT COUNT(`num`),
        CASE  WHEN (`pl_de_manutencao` = 'Liberado' || `pl_de_manutencao` = 'N/A'
            AND `ra_padrao` != 'Liberado' || `ra_padrao` != 'N/A') 
        THEN COUNT(`num`) END AS `Count` 
FROM `ind_master_view`

something like this code.

  • Try edit and add expected results and their table structure, something like this question.

  • already placed to group and it measures the values, however I need the total value

1 answer

1

If that’s what I mean, you have to use COUNT() before the 1 increment case if the condition is true .

SELECT COUNT(CASE  WHEN (pl_de_manutencao = 'Liberado' || pl_de_manutencao = 'N/A' 
                        AND ra_padrao != 'Liberado' || ra_padrao != 'N/A') 
                    THEN 1  -- caso a condição seja verdadeira .
            END) AS Total
FROM ind_master_view

inserir a descrição da imagem aqui

Browser other questions tagged

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