How can I count fields that are NULL?

Asked

Viewed 3,503 times

1

I’m using this query: I would like to know how to count these fields that have no records. Here returns 0. But I have some null records. What I do?

SELECT DataFim, COUNT(DataFim) AS QTD FROM AtivacaoGuincho
GROUP BY DataFim
HAVING DataFim is  null
  • If you can, try SELECT COUNT(*) - COUNT(DataFim) AS qtd and tell me if it works.

2 answers

8


Thus:

SELECT COUNT(*) AS QTD FROM AtivacaoGuincho
WHERE DataFim is  null

Since Marlon brought up the subject of line accounting I can improve on a little more that answer and get into a subject that is almost a curiosity.

Count is basically used in this way above. I request that you count all lines WHERE THE END IS NULL.

But I could do so:

SELECT COUNT(*) AS TOTAL, COUNT(DATAFIM) AS DTFIM FROM AtivacaoGuincho

inserir a descrição da imagem aqui

And it would have something like this: I have 1884 rows in the table and 50 rows where DATAFIM is null.

And of course, I could have as many Counts as I wanted in my select.

  • this returns me 2. It is correct

6

Missing the Where clause correctly..

SELECT COUNT(DataFim) AS QTD 
FROM AtivacaoGuincho
WHERE DataFim is  null

EDIT:

Sorry for the mistake of Ctrl+c/Ctrl+v, the correct is:

SELECT COUNT(*) AS QTD 
FROM AtivacaoGuincho
WHERE DataFim is null

And for those who don’t know the difference between Count(*) and Count(coluna) is that:

  • Count(*) accounts for all lines without exceptions.
  • Count(coluna) accounts for values of coluna who are not null (null)
  • this touches me 0

  • @Aline, I edited the answer with information that may be valuable to you :)

  • Oops, thank you, boy.

  • Awwwwwwwwwwwwwwwwwwwww got it

  • Now I know why I was returning 0...

Browser other questions tagged

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