SQL query return reset value where COUNT finds null

Asked

Viewed 510 times

0

Hello, I have a table of projects that contains status, responsible and other columns. I’m doing the following consultation,

  SELECT status,COUNT(status) AS Total 
  FROM projetos 
  WHERE responsavel='Teste' 
  GROUP BY status ORDER BY status;

Then returns only the status that that responsavel has and I would need it to return all status but with the COUNT(status) with zero value.

This way the table comes this way:

+-----------+-------+
| status    | total |
+-----------+-------+
| Cancelado |     1 |
+-----------+-------+

And I need it to come from the following:

+-------------+-------+
| status      | total |
+-------------+-------+
| Cancelado   |     1 |
| Finalizado  |     0 |
| Em processo |     0 |
| Parado      |     0 |
| Atrasado    |     0 |
+-------------+-------+

The responsible named test has only one project in which the status is as Canceled.

  • The query with LEFT OUTER JOIN does exactly what you want.

  • But it’s just a single table I have, as it would look ?

  • So how does your DBMS know what are all possible status? Or does this list exist only in your head?

  • It is relative, a user can come and register a project with any status he wants, but in the beginning they are those of the example.

1 answer

1

Try the coalesce function that serves exactly for this:

COALESCE(COUNT(status), 0) AS Total

================

If you have a possible status table then use a LEFT OUTER JOIN:

SELECT tabela_de_status.status, COALESCE(COUNT(projetos.status), 0) AS Total 
  FROM tabela_de_status LEFT OUTER JOIN ON (tabela_de_status. status = projetos.status)
  WHERE responsavel='$Nome_de_Algum_responsavel' 
  GROUP BY tabela_de_status.status ORDER BY tabela_de_status.status;

===============================

To consider all existing status in the projects table:

SELECT tabela_de_status.status, COALESCE(COUNT(projetos.status), 0) AS Total 
  FROM (SELECT DISTINCT status FROM projetos) AS tabela_de_status LEFT OUTER JOIN ON (tabela_de_status. status = projetos.status)
  WHERE responsavel='$Nome_de_Algum_responsavel' 
  GROUP BY tabela_de_status.status ORDER BY tabela_de_status.status;

But I would say that your database is not well structured.

  • SELECT status,COALESCE(COUNT(status),0) AS total FROM projects WHERE responsavel='Test' GROUP BY status ORDER BY status; .

  • Try as follows: COALESCE(NULLIF(COUNT(status),0,0))

  • You mean there are status values that do not exist in your projects table?

  • In my table it has several status: Finished, In process, Late,etc. But I have to have a query that determined responsible when there is no project in any status appear with the value zeroed. Edvaldo gave the following error: ERROR 1582 (42000): Incorrect Parameter Count in the call to Native Function 'NULLIF'

  • Sorry, I thought it was SQL Server ...

  • I’m using mysql in XAMPP, some other idea ?

Show 1 more comment

Browser other questions tagged

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