Aggregate functions ignore null values.
So
SELECT COUNT(cola) AS thecount
FROM tablea
is equivalent to
SELECT count(*) AS thecount
FROM tablea
WHERE cola IS NOT NULL;
As all of your values are null, count(cola) has to return zero.
If you want to count the rows that are null, you need count(*)
SELECT cola,
count(*) AS theCount
FROM tablea
WHERE cola is null
GROUP BY cola;
Or simpler:
SELECT count(*) AS theCount
FROM tablea
WHERE cola is null;
If you want to count NULL and NOT NULL values in a single query, use:
SELECT count(cola) as not_null_count,
count(case when cola is null then 1 end) as null_count
FROM tablea;
Answer from user1822 on Stack ExchangeAggregate functions ignore null values.
So
SELECT COUNT(cola) AS thecount
FROM tablea
is equivalent to
SELECT count(*) AS thecount
FROM tablea
WHERE cola IS NOT NULL;
As all of your values are null, count(cola) has to return zero.
If you want to count the rows that are null, you need count(*)
SELECT cola,
count(*) AS theCount
FROM tablea
WHERE cola is null
GROUP BY cola;
Or simpler:
SELECT count(*) AS theCount
FROM tablea
WHERE cola is null;
If you want to count NULL and NOT NULL values in a single query, use:
SELECT count(cola) as not_null_count,
count(case when cola is null then 1 end) as null_count
FROM tablea;
This is by design.
COUNT(<expression>) counts rows where the <expression> is not null.
COUNT(*) counts rows.
So, if you want to count rows, use COUNT(*).
You can't select the values from the table when the row count is 0. Where would it get the values for the nonexistent rows?
To do this, you'll have to have another table that defines your list of valid Project and Financial_Year values. You'll then select from this table, perform a left join on your existing table, then do the grouping.
Something like this:
SELECT l.Project, l.Financial_Year, COUNT(t.Project) AS HighRiskCount
INTO #HighRisk
FROM MasterRiskList l
left join #TempRisk1 t on t.Project = l.Project and t.Financial_Year = l.Financial_Year
WHERE t.Risk_1 = 3
GROUP BY l.Project, l.Financial_Year
Wrap your SELECT Query in an ISNULL:
SELECT ISNULL((SELECT Project, Financial_Year, COUNT(*) AS hrc
INTO #HighRisk
FROM #TempRisk1
WHERE Risk_1 = 3
GROUP BY Project, Financial_Year),0) AS HighRiskCount
If your SELECT returns a number, it will pass through. If it returns NULL, the 0 will pass through.
Hello,
This is my query
SELECT ordertype, status, sum (COUNT (printdate)) over (), date(char(1900000+requestdate)), businessunit FROM Casepallet WHERE date(char(1900000+requestdate)) IN (current date, current date + 1 days) AND business unit=' SCS' AND ordertype!='T1' AND status = 'X' group by ordertype, business unit, request date, status, printdate order by printdate desc limit 1
Sometimes the sum/count of printdate returns nothing, it's null. If it's null I want it to return 0.
I tried doing it like this: COALESCE (sum (COUNT (printdate)) over (), 0), but it doesn't work, it still returns null.
Can anyone guide me here on where I go wrong?
In the outer query, you can replace a NULL with a zero using the IFNULL() function, e.g.
SELECT ...
, IFNULL(v.t1count,0) AS t1count
FROM ...
LEFT
JOIN ( SELECT ... AS t1count
...
) v
ON ...
The NULL you are getting returned by the outer query isn't from the inline view query. The NULL is a result of "no match" being found by the LEFT [OUTER] JOIN operation.
If you are referencing v.t1count in other expressions in the outer query, you can replace those references with NULLIF(v.t1count,0) as well.
The aggregate COUNT() will always return a value.
Reference: Does COUNT(*) always return a result?
This works for Oracle and SQL Server (you might be able to get it to work on another RDBMS):
select sum(case when a is null then 1 else 0 end) count_nulls
, count(a) count_not_nulls
from us;
Or:
select count(*) - count(a), count(a) from us;
If I understood correctly you want to count all NULL and all NOT NULL in a column...
If that is correct:
SELECT count(*) FROM us WHERE a IS NULL
UNION ALL
SELECT count(*) FROM us WHERE a IS NOT NULL
Edited to have the full query, after reading the comments :]
SELECT COUNT(*), 'null_tally' AS narrative
FROM us
WHERE a IS NULL
UNION
SELECT COUNT(*), 'not_null_tally' AS narrative
FROM us
WHERE a IS NOT NULL;
The reason for that is because you have provided count with a column value which is null. Instead, use count(*):
SELECT COUNT(*) FROM Table_Name WHERE [Current_Status] IS NULL
Sample data:
current_status
--------------
Processed
Null
Not Processed
Null
And the difference between two queries:
count(current_status)
SELECT count(current_status) FROM table_name WHERE current_status IS NULL
0
count(*)
SELECT count(*) FROM table_name WHERE current_status IS NULL
2
With
SELECT COUNT([Current_Status]) FROM Table_Name WHERE [Current_Status] IS NULL
you say "take all rows where the current_status is null and count how many of these current_status are not null". Which is zero of course. COUNT(<expression>) counts non-null occurrences of the expression.
You want to count rows instead:
SELECT COUNT(*) FROM table_name WHERE current_status IS NULL;
Or count remains:
SELECT COUNT(*) - COUNT(current_status) FROM table_name;