T-SQL COUNT(*) counts NULLS but I don't want to count NULLS in row count - sql

I have a scenario where I'm trying not to count a row where it has a zero, blank or NULL. But I'm not sure how to. I have used ISNULL to replace it with blank but the result shows zero. I don't want zero because it messes up my averages etc. The screen shot below should show 17 in the bottom as total but it's showing 18 because it's counting the null as row count. This null row has an N/A as a value in the data set but my count counts it as a row. I'm using count() because I have many other columns so I can not change the count(). Any ideas on how to show the total as 17 instead of 18?
Thank you

Some SQL implementations (I think this is also proper ANSI standard but don't know that for sure) exhibit a different behaviour for COUNT(*) VS COUNT(field).
The former will include NULLs, the latter will exclude them.

you can use SUM like this
SELECT SUM(CASE WHEN ISNULL(testeq4,'') <> '' THEN 1 ELSE 0 end)
FROM YourTable

Try this:
SELECT SUM(CASE WHEN ISNULL(TestQ4,0)=0 THEN 0 ELSE 1 END)

Related

How to count records from multiple columns eliminating null values in hive table

I'm using the below command to find the sum of records from 8 columns but getting null in the O/P as shown below.
Command part 1
command part 2
Output
How can this be fixed?
Yes, the thing is NULL + something results NULL. To solve this, wrap each sum() in the NVL(sum(),0), so if some particular sum() is NULL, it is converted to 0 and the whole total will be not null:
nvl(sum(case when col1='something' then 1 else 0 end),0)+ ...
Or always use else 0, like in the first expression (H).
Wrapping with NVL() will solve the problem even if column comes from the join and the rows are absent and sum is NULL.

Count() Specifying Uncounted Value?

Using Microsoft SQL Server, if you use COUNT(column name) it returns the number of rows in that column which have a non-null value (i.e., it counts the rows, ignoring nulls).
Is there any way to do something similar, but allowing you to tell it which values to ignore? For example, if I wanted to count all the rows in a table which have a value which is NOT 1, I could do something like COUNTNOT(column name,1). That would count all the rows in the specified column which have a value NOT 1.
You may use conditional aggregation:
SELECT COUNT(CASE WHEN some_val <> 1 THEN 1 END) AS cnt
FROM yourTable;
The above logic is that COUNT will count one whenever some value is not equal to 1. Otherwise, it falls on the ELSE conditional, which if not present defaults to the value NULL. Since NULL is not counted, any value other than 1 would contribute zero to the count.
Why not put what you want to exclude in a WHERE clause?
SELECT COUNT(some_val) AS cnt
FROM yourTable
WHERE some_val <> 1
You need to be careful about NULL values. I would recommend:
select sum(case when column in (<values to ignore>) then 0 else 1 end)
This will count NULL values as not in the list (even if NULL is in the list). To ignore NULL values (as well), switch the logic to:
select sum(case when column not in (<values to ignore>) then 1 else 0 end)
and be sure NULL is not in the list.

Selective Summation inside the case statement

I am using sum function inside case statement. I want to do selective summation of rows.
case when
type in ('A','B')
then nvl(SUM(Amount_1),Amount)
else Amount
Two columns are there Amount and Amount_1 and for the Amount_1 column, I want to remove few rows based on some condition C1. For e.g. If I have 10 rows that have type in A and based on condition C1 I am getting only 8 rows then I want to sum Amount_1 column on the basis of 8 rows only.
I think you have the basic logic understood correctly, but your syntax is a bit off. You should be using the CASE expression inside the SUM() function, not the other way around.
SELECT type,
SUM(CASE WHEN type IN ('A', 'B')
THEN COALESCE(Amount_1, Amount) ELSE Amount END) AS type_sum -- ELSE 0 ?
FROM yourTable
This will compute the sum of COALESCE(Amount_1, Amount) for those records where the type is either A or B, otherwise it will use Amount in the sum. If you intended to not count non-matching records at all, then modify my query by using ELSE 0 in the CASE expression.

SQL (SQLite) count for null-fields over all columns

I've got a table called datapoints with about 150 columns and 2600 rows. I know, 150 columns is too much, but I got this db after importing a csv and it is not possible to shrink the number of columns.
I have to get some statistical stuff out of the data. E.g. one question would be:
Give me the total number of fields (of all columns), which are null. Does somebody have any idea how I can do this efficiently?
For one column it isn't a problem:
SELECT count(*) FROM datapoints tb1 where 'tb1'.'column1' is null;
But how can I solve this for all columns together, without doing it by hand for every column?
Best,
Michael
Building on Lamak's idea, how about this idea:
SELECT (N * COUNT(*)) - (
COUNT(COLUMN_1)
+ COUNT(COLUMN_2)
+ ...
+ COUNT(COLUMN_N)
)
FROM DATAPOINTS;
where N is the number of columns. The trick will be in making the summation series of COUNT(column), but that shouldn't be too terrible with a good text editor and/or spreadsheet.
i don't think there is an easy way to do it. i'd get started on the 150 queries. you only have to replace one word (column name) each time.
Well, COUNT (and most aggregations funcions) ignore NULL values. In your case, since you are using COUNT(*), it counts every row in the table, but you can do that on any column. Something like this:
SELECT TotalRows-Column1NotNullCount, etc
FROM (
SELECT COUNT(1) TotalRows,
COUNT(column1) Column1NotNullCount,
COUNT(column2) Column2NotNullCount,
COUNT(column3) Column3NotNullCount ....
FROM datapoints) A
To get started it's often helpful to use a visual query tool to generate a field list and then use cut/paste/search/replace or manipulation in a spreadsheet program to transform it into what is needed. To do it all in one step you can use something like:
SELECT SUM(CASE COLUMN1 WHEN NULL THEN 1 ELSE 0 END) +
SUM(CASE COLUMN2 WHEN NULL THEN 1 ELSE 0 END) +
SUM(CASE COLUMN3 WHEN NULL THEN 1 ELSE 0 END) +
...
FROM DATAPOINTS;
With a visual query builder you can quickly generate:
SELECT COLUMN1, COLUMN2, COLUMN3 ... FROM DATAPOINTS;
You can then replace the comma with all the text that needs to appear between two field names followed by fixing up the first and last fields. So in the example search for "," and replace with " WHEN NULL 1 ELSE 0 END) + SUM(CASE " and then fix up the first and last fields.

Avoiding the null values to replace 0 values in report

I am using SQL Server 2005 BOXIR2.
My doubt, from universe table there is an eventcode having different types of codes like Enquiry,FollowUp,LostofSales,Contact,etc
I make a measure that is from object properties formula count(Tablename.EventCode)save and export it, when I used this EventCode in Webireport, it show values for paricular EventCode, but zero values are not read it show null blank as below example .
I WANT TO GET THE ZERO VALUES FOR WHICH IT IS IN BLANK(NULL).
count(Tablename.EventCode)
Enquiry,FollowUp,LostofSales,Contact
10 20 15
5 12 5
6 4 3
Can u please help me how to get get zero values for null,Formula
I'm not sure exactly what you are asking, but I think you may be looking for ISNULL()
SELECT ISNULL(table_name.column_name, 0)
will return 0 if table_name.column_name is null
If you're getting NULLs when performing an aggregate, it's probably because one of elements is NULL. All you need to do is coalesce those entries to a known value (such as zero).
SELECT COUNT(COALESCE(Tablename.EventCode, 0)) FROM Tablename