"0" वाली एंट्रीज को बाहर रखने के लिए, आपको AVG() फंक्शन के साथ NULLIF() का उपयोग करना होगा।
सिंटैक्स निम्नलिखित है:
SELECT AVG(NULLIF(yourColumnName, 0)) AS anyAliasName FROM yourTableName;
आइए पहले एक टेबल बनाते हैं:
mysql> create table AverageDemo
-> (
-> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> StudentName varchar(20),
-> StudentMarks int
-> ); Query OK, 0 rows affected (0.72 sec)
INSERT कमांड का उपयोग करके टेबल में कुछ रिकॉर्ड डालें।
क्वेरी निम्नलिखित है:
mysql> insert into AverageDemo(StudentName,StudentMarks) values('Adam',NULL); Query OK, 1 row affected (0.12 sec)
mysql> insert into AverageDemo(StudentName,StudentMarks) values('Larry',23); Query OK, 1 row affected (0.19 sec)
mysql> insert into AverageDemo(StudentName,StudentMarks) values('Mike',0); Query OK, 1 row affected (0.20 sec)
mysql> insert into AverageDemo(StudentName,StudentMarks) values('Sam',45); Query OK, 1 row affected (0.18 sec)
mysql> insert into AverageDemo(StudentName,StudentMarks) values('Bob',0); Query OK, 1 row affected (0.12 sec)
mysql> insert into AverageDemo(StudentName,StudentMarks) values('David',32); Query OK, 1 row affected (0.18 sec)
SELECT स्टेटमेंट का उपयोग करके टेबल से सभी रिकॉर्ड प्रदर्शित करें।
क्वेरी निम्नलिखित है:
mysql> select * from AverageDemo;
निम्नलिखित आउटपुट है:
+----+-------------+--------------+ | Id | StudentName | StudentMarks | +----+-------------+--------------+ | 1 | Adam | NULL | | 2 | Larry | 23 | | 3 | Mike | 0 | | 4 | Sam | 45 | | 5 | Bob | 0 | | 6 | David | 32 | +----+-------------+--------------+ 6 rows in set (0.00 sec)
AVG का उपयोग करते समय "0" वाली एंट्रीज को बाहर रखने की क्वेरी निम्नलिखित है:
mysql> select AVG(NULLIF(StudentMarks, 0)) AS Exclude0Avg FROM AverageDemo;
निम्नलिखित आउटपुट है:
+-------------+ | Exclude0Avg | +-------------+ | 33.3333 | +-------------+ 1 row in set (0.05 sec)