I am trying to find is there is an efficient way to do a query which can ignore rows of data where any column has a value of 0 or empty
My current query looks like this:
$query = "SELECT AVG(concept1) AS c1, AVG(concept2) AS c2, AVG(concept3) AS c3, AVG(concept4) AS c4, AVG(concept5) AS c5, AVG(concept6) AS c6 FROM KeyPad";
Now I dont want to count rows while averaging where any column data is 0 or null.
eg.
-----------------------------------------
|concept1 | concept2 | concept3 | ...
-----------------------------------------
| 1.2 | 3.2 | 0 | ...
| 2.4 | 4.1 | 2.1 | ...
| 3.2 | 5.1 | 2.2 | ...
| 4.3 | 3.2 | 5.3 | ...
| 1.2 | 0 | 3.2 | ...
------------------------------------------
When I run my query, I only want to generate average using data from rows 2,3,4 and ignore all data from rows 1 and 5.
I read that I can use something like COALESCE but not sure if it can work for averaging values.
Any suggestions which direction should I explore?
I may not have explained correctly. When I say ignore rows, I mean if one item inn the row is 0, I want to ignore all the other items in the rows while averaging those columns too.
So in the example table above, the average for Column1 will be: (2.4+3.2+4.3) / 3 because First row in column 3 is 0 and last row in column 2 is also 0. So other columns will also need to ignore these two rows while calculating the average.
I hope that clarifies my problem.
To look for NULL values, you must use the IS NULL test. The following statements show how to find the NULL phone number and the empty phone number: mysql> SELECT * FROM my_table WHERE phone IS NULL; mysql> SELECT * FROM my_table WHERE phone = ''; See Section 3.3.
The IS NULL constraint can be used whenever the column is empty and the symbol ( ' ') is used when there is empty value. mysql> SELECT * FROM ColumnValueNullDemo WHERE ColumnName IS NULL OR ColumnName = ' '; After executing the above query, the output obtained is.
Select and Filter Data From a MySQL Database The WHERE clause is used to filter records. The WHERE clause is used to extract only those records that fulfill a specified condition.
Below is the syntax to filter the rows without a null value in a specified column. Syntax: SELECT * FROM <table_name> WHERE <column_name> IS NOT NULL; Example: SELECT * FROM demo_orders WHERE ORDER_DATE IS NOT NULL; --Will output the rows consisting of non null order_date values.
You can use like this, it will not consider row with value 0
and AVG
itself excludes null
values
SELECT AVG(NULLIF(concept4, 0)) FROM KeyPad;
or using case (sample demo link)
SELECT AVG(case
when concept1 = 0 then null
else concept1
end) as c1
FROM KeyPad
so your full query:
SELECT AVG(NULLIF(concept1,0)) AS c1,
AVG(NULLIF(concept2,0)) AS c2,
AVG(NULLIF(concept3,0)) AS c3,
AVG(NULLIF(concept4,0)) AS c4,
AVG(NULLIF(concept5,0)) AS c5,
AVG(NULLIF(concept6,0)) AS c6
FROM KeyPad
If you want you can add condition in WHERE
similar like this according to your requirement:
SELECT AVG(concept1) AS c1
FROM KeyPad
WHERE concept1 > 0
Note: Where condition may change your desired output if grouped wrongly with more than one column. If you want to check for individual field then use
NULLIF
orCASE
If I am getting you right, Below query will help you:
$query = "SELECT AVG(concept1) AS c1, AVG(concept2) AS c2, AVG(concept3) AS c3 FROM KeyPad where (concept1 IS NOT NULL OR concept1 != 0) AND (concept2 IS NOT NULL OR concept2 != 0) AND (concept3 IS NOT NULL OR concept3 != 0");
Here Just add till concept3 you can do with more columns.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With