I have a table
num
----
NULL
NULL
NULL
NULL
55
NULL
NULL
NULL
99
when I wrote
select COUNT(*)
from tbl
where num is null
the output was 7
but when I wrote
select COUNT(num)
from tbl
where num is null
the output was 0
what's the difference between these two queries ??
Difference is in the field you select.
When counting COUNT(*) NULL values are taken into account (count all rows returned).
When counting COUNT(num) NULL values are NOT taken into account (count all non-null fields).
That is a standard behavior in SQL, whatever the DBMS used
Source. look at COUNT(DISTINCT expr,[expr...])
count(*) returns number of rows, count(num) returns number of rows where num is not null. Change your last query to select count(*) from test where num is null to get the result you expect.
In second case first count values are eliminated and then where clause comes in picture. While in first case when you are using * row with null is not eliminated.
If you are counting on a coll which contains null and you want rows with null to be included in count than use
Count(ISNULL(col,0))
Count(*) counts the number of rows, COUNT(num) counts the number of not-null values in column num.
Considering the output given above, the result of the query count(num) should be 2.
Related
i am trying to do a count on null values however , it is not able to count null values.
example of table :
Country Id Id_typ Info
Us 123 NULL Testing
Us 124 NULL Testing
Us 125 Bob testing
this is my script to count null values
select count(id_typ) from combined_a where id_typ= 'NULL' limit 1
i have tried
select count(id_typ) from table_a where id_typ is null limit 1
however when i have change the condition to search id_typ = bob, it was able to do a count . i am unsure on what did i do wrong , any advice?
You need is null and count(*):
select count(*)
from table_a
where id_typ is null;
limit 1 is redundant. A SQL query with an aggregation function and no group by always returns one row.
I have a database called people with a total of 8398 records. I know this because I have performed the following query with the COUNT function.
SELECT COUNT(*)
FROM people;
However, when I perform a COUNT on the birthdate column I find that this only returns 6152 records. Is this because the COUNT function is automatically excluding NULL values in the birthdate column?
SELECT COUNT(birthdate)
FROM people;
How could I perform a count of all the columns that are NULL in the birthday column?
How could I perform a count of all the columns that are NULL in the
birthday column?
Check if column is null using CASE expression:
SELECT COUNT(CASE WHEN birthdate IS NULL THEN 1 END)
FROM people;
The expression evaluates to 1 when birthdate is null, and evaluates to NULL when brthdate is not null. Since COUNT counts only not null values, you will get a number of NULLs in birthdate column.
please check this solution its might be helpful..
SELECT COUNT(CASE WHEN birthdate IS NULL THEN 1 END)
FROM people;
In TSql what is the recommended approach for grouping data containing nulls?
Example of the type of query:
Select Group, Count([Group])
From [Data]
Group by [Group]
It appears that the count(*) and count(Group) both result in the null group displaying 0.
Example of the expected table data:
Id, Group
---------
1 , Alpha
2 , Null
3 , Beta
4 , Null
Example of the expected result:
Group, Count
---------
Alpha, 1
Beta, 1
Null, 0
This is the desired result which can be obtained by count(Id). Is this the best way to get this result and why does count(*) and count(Group) return an "incorrect" result?
Group, Count
---------
Alpha, 1
Beta, 1
Null, 2
edit: I don't remember why I thought count(*) did this, it may be the answer I'm looking for..
The best approach is to use count(*) which behaves exactly like count(1) or any other constant.
The * will ensure every row is counted.
Select Group, Count(*)
From [Data]
Group by [Group]
The reason null shows 0 instead of 2 in this case is because each cell is counted as either 1 or null and null + null = null so the total of that group would also be null. However the column type is an integer so it shows up as 0.
Just do
SELECT [group], count([group])
GROUP BY [group]
SQL Fiddle Demo
Count(id) doesn't gives the expected result as mentioned in question. Gives value of 2 for group NULL
try this..
Select Group, Count(isNull(Group,0))
From [Data]
Group by [Group]
COUNT(*) should work:
SELECT Grp,COUNT(*)
FROM tab
GROUP BY Grp
One more solution could be following:
SELECT Grp, COUNT(COALESCE(Grp, ' '))
FROM tab
GROUP BY Grp
Here is code at SQL Fiddle
I am not able to understand the SQL query output :
SQL> select distinct(STATUS) from TMP_ORDER_ACTION_PSTN_CP_11035;
InDelivery_SOMBe
In Delivery
Complete
Amended
Cancelled
Failed InComplete
1 SQL> select count(*) from TMP_ORDER_ACTION_PSTN_CP_11035 where
STATUS='Complete';
1484
2 SQL> select count(*) from TMP_ORDER_ACTION_PSTN_CP_11035 where STATUS
!= 'Complete';
3167
3 SQL> select count(*) from TMP_ORDER_ACTION_PSTN_CP_11035;
5091
The sum of count for the 1 and 2 queries should be same as the total count(3 query).Why is the sum differing from the whole count?
It seems like a dump question but i dont know why is this happening.
Please note that My question is not related to null check at all.It is the that
sum(1+2)=3.1484+3167 !=5091.Why is the result different?
My guess is NULL values, which match none of your WHERE clauses, including the last one. Try
select count(*) from TMP_ORDER_ACTION_PSTN_CP_11035 where STATUS is null;
where status = null is never true, nor is where null = null. You have to use is null.
The sum of count for the 1 and 2 queries should be same as the total count(3 query).Why is the sum differing from the whole count?
No, because the records with NULL are not matching query 1 or query 2, but they are counted in query 3.
1 + 2 + IS NULL should equal 3.
WHERE STATUS = NULL won't work. Nothing equals NULL.
Try IS instead of =...
select count(*) from TMP_ORDER_ACTION_PSTN_CP_11035 where STATUS IS null
Try this:
Assuming p_key is a primary key for the table,
select count(p_key) from TMP_ORDER_ACTION_PSTN_CP_11035 where STATUS='Complete';
select count(p_key) from TMP_ORDER_ACTION_PSTN_CP_11035 where STATUS <> 'Complete';
select count(p_key) from TMP_ORDER_ACTION_PSTN_CP_11035 ;
I have a table with a column that allows nulls. If the value is null it is incomplete. I want to calculate the percentage complete.
Can this be done in MySQL through SQL or should I get the total entries and the total null entries and calculate the percentage on the server?
Either way, I'm very confused on how I need to go about separating the variable_value so that I can get its total results and also its total NULL results.
SELECT
games.id
FROM
games
WHERE
games.category_id='10' AND games.variable_value IS NULL
This gives me all the games where the variable_value is NULL. How do I extend this to also get me either the TOTAL games or games NOT NULL along with it?
Table Schema:
id (INT Primary Auto-Inc)
category_id (INT)
variable_value (TEXT Allow Null Default: NULL)
When you use "Count" with a column name, null values are not included. So to get the count or percent not null just do this...
SELECT
count(1) as TotalAll,
count(variable_value) as TotalNotNull,
count(1) - count(variable_value) as TotalNull,
100.0 * count(variable_value) / count(1) as PercentNotNull
FROM
games
WHERE
category_id = '10'
SELECT
SUM(CASE WHEN G.variable_value IS NOT NULL THEN 1 ELSE 0 END)/COUNT(*) AS pct_complete
FROM
Games G
WHERE
G.category_id = '10'
You might need to do some casting on the SUM() so that you get a decimal.
To COUNT the number of entries matching your WHERE statement, use COUNT(*)
SELECT COUNT(*) AS c FROM games WHERE games.variable_value IS NULL
If you want both total number of rows and those with variable_value being NULL in one statement, try GROUP BY
SELECT COUNT(variable_value IS NULL) AS c, (variable_value IS NULL) AS isnull FROM games GROUP BY isnull
Returns something like
c | isnull
==============
12 | 1
193 | 0
==> 12 entries have NULL in that column, 193 havn't
==> Percentage: 12 / (12 + 193)