Mysql AVG to ignore zero - mysql

I need to perform an avg on a column, but I know that most of the values in that column will be zero. Out of all possible rows, only two will probably have positive values. How can I tell mySQL to ignore the zeros and only average the actual values?

Assuming that you might want to not totally exclude such rows (perhaps they have values in other columns you want to aggregate)
SELECT AVG(NULLIF(field ,0))
from table

You could probably control that via the WHERE clause:
select avg( field ) from table where field > 0

select avg(your_column)
from your_table
where your_column != 0

You can convert zeros to NULL, then AVG() function will work only with not NULL values.
UPDATE table SET column = NULL WHERE column='0';
SELECT AVG(column) FROM table;

Related

Empty column with float datatype returns 1 value

I would like to ask something, please be good to me.
This is one of my column fields with its datatype.
column_name: amount_paid
datatype: float(13,2)
When I count this column, it returns value 1 even if there's no existing value or inserted on it.
Before Query:
After Query:
Query used: SELECT COUNT(amount_paid), COUNT(amount_unpaid) from transactions
I want to achieved a Zero(0) value because of my own reason.
Please be good to me, thank you!
I think you need sum not count.
count is use to count rows doc
sum is use to accumulate the value of the field doc, you might need to use a group by clause
SELECT SUM(amount_paid), SUM(amount_unpaid) from transactions
Has to be null to count like Cero you can do something like this
UPDATE table SET column = NULL WHERE test = "example";

How to select zero result mysql?

I want to return a select query with zero result. When I type this query:
Select NULL .... from ....
It returns NULL value as a result. But I want to return zero result instead of NULL. So, how to do that? I use coalesce but if I have value, it doesn't return zero. So, what should select query use to return zero value? I use MYSQL.
If you want to add column to existing columns you can use the below instead.
Select *,0 as ZeroColumn from ....
I can't imagine why you want to do this, but if you do it's quite simple. SELECT 0 will return zero once. SELECT 0 FROM MyTable will return zero once for each record in MyTable.

max() in mysql returns 9999

i have a database table with unique column containing value like
invid
----------
500
1000
B2222
A9998
A9999
A10000
the problem whenever my query max(invid) is return A9999 what is the solution
try this
SELECT CONCAT('A', MAX(0+SUBSTRING(invid,2))) FROM your_table
The problem is you're trying to find the max value of the alphanumeric, in this case A9999 is greater than A10000, it's not the same with 9999 and 10000.
The best solution is to correct the problem permanently by changing the column to an auto_increment and remove the A letter.
Otherwise you could use following query:
SELECT CONCAT('A'
, MAX(0+SUBSTRING(invid,2)))
FROM table
You can SELECT the maximum of the numerical value in the invid column:
SELECT t.invid, MAX(t.value) AS maxValue
FROM
(
SELECT invid,
CASE WHEN invid LIKE '[A-Z]%'
THEN CAST(SUBSTRING(invid, 2) AS UNSIGNED)
ELSE CAST(invid AS UNSIGNED)
END AS value
FROM yourTable
) t
This assumes that each entry in invid will have at most one letter prefixing the number. This does not take into account the possibility of ties for maximum value.

checking to ensure all values in a field are integers in MySQL

I have a column that is currently a floating-point number and I need to check if all the values in the column are integers. What's the easiest way to do this?
SELECT COUNT(*) FROM yourtable WHERE ceil(yourcolumn) != yourcolumn
If the count > 0 then there are non-integer values.
And to specifically find the records that are not integers...
SELECT * from yourtable WHERE col % 1 != 0;

MySQL COUNT() and nulls

Am I correct in saying:
COUNT(expr)
WHERE expr IS NOT *
Will count only non nulls?
Will COUNT(*) always count all rows? And What if all columns are null?
Correct. COUNT(*) is all rows in the table, COUNT(Expression) is where the expression is non-null only.
If all columns are NULL (which indicates you don't have a primary key, so this shouldn't happen in a normalized database) COUNT(*) still returns all of the rows inserted. Just don't do that.
You can think of the * symbol as meaning "in the table" and not "in any column".
This is covered in the MySQL Reference Manual.
If you want to count NULLs as well, try
SELECT COUNT(IFNULL(col, 1)) FROM table;
just checked:
select count(*)
returns 1 with one record filled with NULLs
select count(field)
returns 0.
I don't see the point in the record with NULL values. Such record must not exist.
count(*) is not for non-null columns, it's just the way to ask to count all rows. Roughly equivalent to count(1).
Using MySQL I found this simple way:
SELECT count(ifnull(col,1)) FROM table WHERE col IS NULL;
This way will not work:
SELECT count(col) FROM table WHERE col IS NULL;
If you want to count only the nulls you can also use COUNT() with IF.
Example:
select count(*) as allRows, count(if(nullableField is null, 1, NULL)) as missing from myTable;
You can change the if condiditon to count what you actually want. So you can have multiple counts in one query.
select count(*) as 'total', sum(if(columna is null, 1, 0)) as 'nulos' from tabla;