CASE with IS NULL not replacing as expected - mysql

I have a simple column with some numbers, which looks like;
133
8
55
11
NULL
NULL
235
NULL
I want to put a default in when selecting this column, with the aim of replacing the nulls. I've done;
CASE
WHEN round(avg(s.seconds)) IS NULL THEN 0
ELSE round(avg(s.seconds))
END as 'seconds'
However, my nulls remain. My expectation is that they would be set to 0. Why would this not work?

NULL values are ignored in average calculations.
If you want to replace NULL values for 0 in your average, you need to use COALESCE on the values, inside the average function. Example follows:
DROP TEMPORARY TABLE IF EXISTS TEMP1;
CREATE TEMPORARY TABLE TEMP1
SELECT 1 AS price
UNION ALL SELECT NULL AS price
UNION ALL SELECT NULL AS price
UNION ALL SELECT 3 AS price;
SELECT * FROM TEMP1; # List values (1, NULL, NULL, 3)
SELECT AVG(price) FROM TEMP1; # Returns 2 (NULL values IGNORED)
SELECT AVG(COALESCE(price,0)) FROM TEMP1; # Returns 1 (NULL values REPLACED with 0)

You can do it by 3 ways
1
round(avg(CASE
WHEN s.seconds IS NULL THEN 0
ELSE s.seconds
END)) as 'seconds'
2
The MySQL IFNULL() function lets you return an alternative value if an
expression is NULL:
round(avg(IFNULL(s.seconds,0)))
3
or we can use the COALESCE() function, like this:
round(avg(COALESCE(s.seconds,0)))

Related

Check for all null values in a GROUP BY [duplicate]

This question already has answers here:
MySQL AVG() return 0 if NULL
(3 answers)
Closed last month.
I have the following structre
id val
1 ...
.
.
2 ...
.
.
3 null
3 null
3 null
4 ...
.
.
Basically each id has multiple no. of values. And an id has either all values as integers or all values as null
What I want is to perform an aggregate (like AVG) on val group by id. If that id has null values, I want to put 5 there.
#1
SELECT id, (CASE SUM(val) WHEN null THEN 5 ELSE AVG(val) END) AS ac FROM tt GROUP BY id
> executes ELSE even for id = 3
In CASE, there should be an aggregate function that when done on null values give null.
I checked SUM and MAX like
SELECT SUM(val) FROM tt WHERE id = 3
> null
and it gives null here but doesn't work in main statement. I guess it is related to the type of equality and hence tried WHEN IS NULL but its a syntax error.
Also, is there some more standard way of indicating group of values as all null rather than using SUM or MAX.
You can use if condition :
select id, If(sum(val) is null, 5, AVG(val)) as average
FROM tt
group by id
check here : https://dbfiddle.uk/Uso9nNTM
The exact problem with your CASE expression is that to check for null in MySQL we have to use IS NULL rather than equality. So use this version:
CASE WHEN SUM(val) IS NULL THEN 5 ELSE AVG(val) END
But we might as well just use COALESCE() to assign an average of 5 for those id groups having all null values.
SELECT id, COALESCE(AVG(val), 5) AS avg_val
FROM tt
GROUP BY id;
Note that the AVG() function by default ignores nulls. Therefore, the expression AVG(val) would only be null if every record in an id group were having null for val.

Adding columns in MySQL - how to treat NULL as 0

I have simple table (test) where I need to perform some additions (I want to get a row total and a column total).
id var1 var2
1 NULL NULL
2 10 NULL
For column totals, summing works as expected (NULL is ignored in the addition):
SELECT SUM(var1) FROM test
10
For row totals, addition does not ignore NULL (if any column is NULL the result is NULL):
SELECT var1+var2 FROM test
NULL
NULL
What I want it to return is:
SELECT var1+var2 FROM test
NULL
10
Is there a way to get MySQL to treat NULL as 0 in an addition?
You want to use coalesce():
select coalesce(var1, 0) + coalesce(var2, 0)
coalesce() is ANSI standard and available in most databases (including MySQL).
use the IFNULL function
SELECT IFNULL(var1, 0) + IFNULL(var2, 0) FROM test
select
Case when (coalesce(`var1`, 0) + coalesce(`var2`, 0))=0
then NULL
else coalesce(`var1`, 0) + coalesce(`var2`, 0)
end
from test
SQL FIDDLE

SUM of each row is counting null values as 0. How do I make mysql skip null values?

Well, the values aren't null per say, they are in VARCHAR(30) but in decimal form. However some of the records have "NA" in some fields. I would like mysql to skip those rows in SUM calculation when "NA" is present in the fields used for the SUM. Mysql is treating all incalculable fields as 0. The 0 from fields containing "NA" is misleading. I am doing a GROUP BY TABLE.ID.
Edit:
SELECT
SUM(
CASE
WHEN X >1 THEN 1
WHEN X<-1 THEN 2
ELSE 3
END
CASE
WHEN Y >1 THEN 1
WHEN Y <-1 THEN 2
ELSE 3
END)
AS "Col X+Y";
FROM TableA
GROUP BY TableA_ID;
Sometimes X and/or Y = "NA" on certain fields. I get 6 if both X and Y on TableA_ID = 17 or other numbers when one of them is "NA".
Edit (quoting my comment on VARCHAR):
"I tried storing my values as DEC(5,2), but some of the data from Excel have NA's in the fields. I did set X DEC(5,2) NULL and tried inserting NA into it but kept getting an error (cannot be null). I also tried making the default value "NA" but still get an error (cannot be null). I'll add in a sample query as edit."
I got it. I added in another WHERE clause using
WHERE ..... AND(Colx IS NOT NULL OR Coly IS NOT NULL OR ......);
I switched back the values to DEC(3,1) and made the fields NULLable with defaults null if the field value is NULL. I had to understand how to use NULL. I took out the 'NA's in Excel and left those field values blank.
if foo1 is null or NA just sum it as zero (the neutrum value in the addition), otherwise sum the value.
select sum( case when foo1 is null or foo1 = 'NA' then 0 else foo1 end) as sum, foo2
from FooTable group by foo2
or
select sum(foo1) from FooTable
where (foo2 <> 'NA' and foo2 is null) and (foo3 <> 'NA' or foo3 is null )
group by foo4

Counting total and true condition lines

How to count the number of lines in a table and the number of lines where a certain condition is true without resorting to subselects like this:
create table t (a integer);
insert into t (a) values (1), (2), (null);
select
(select count(*) from t) as total_lines,
(select count(*) from t where a = 1) as condition_true
;
total_lines | condition_true
-------------+----------------
3 | 1
select count(*) as total_lines, count(a = 1 or null) as condition_true
from t
;
total_lines | condition_true
-------------+----------------
3 | 1
It works because:
First while count(*) counts all lines regardless of anything, count(my_column) will count only those lines where my_column is not null:
select count(a) as total
from t
;
total
-------
2
Second (false or null) returns null so whenever my condition is not met it will return null and will not be counted by count(condition or null) which only counts not nulls.
Use SUM(condition)!
select
count(*) as total_lines,
sum(a = 1) as condition_true
from t
See it working here.
This works because in mysql, true is 1 and false is 0, so the sum() of a condition will add 1 when it's true and 0 when it's false - which effectively counts the number of times the condition is true.
Many people falsely believe you need a case statement, but you don't with mysql (you do with some other databases)
this can be easily done using a condition inside count. I don't know if its the optimized method of doing it but it gets the work done
you can do it as follows
select count(*) as total_lines, COUNT(CASE WHEN a = 1 THEN 1 END) as condition_true from t
you can check it here
sqlFiddle

I can't compare sql_variant field with IS NULL operator

I mean I can't query null values when column type sql_variant
For example docsdate table look like this:
ValID DocID Value <--sql variant column)
1. 488 146 30.10.2007
2. 740 190 31.03.2008
3. 570 161 31.10.2008
4. 242 103 NULL
5. 248 104 NULL
When query like select * from docsdate where value is null
no rows returned
Any idea?
This works fine for me. Are you sure your NULL values are actually proper NULLs and not strings containing the text NULL? Do they show up with a yellow background in SSMS?
create table #docsdate
(
ValID int,
DocID int,
value sql_variant
)
INSERT INTO #docsdate
SELECT 488,146,'30.10.2007' UNION ALL
SELECT 740,190,'31.03.2008' UNION ALL
SELECT 570,161,'31.10.2008' UNION ALL
SELECT 242,103,NULL UNION ALL
SELECT 248,104,NULL
SELECT * FROM #docsdate
WHERE value IS NULL
DROP TABLE #docsdate