AVG Function
An aggregate function that returns the average value from a set of numbers. Its single argument can be numeric column, or the numeric result of a function or expression applied to the column value. Rows with a NULL value for the specified column are ignored. If the table is empty, or all the values supplied to AVG are NULL, AVG returns NULL.
When the query contains a GROUP BY clause, returns one value for each combination of grouping values.
Return type: DOUBLE
Examples:
-- Average all the non-NULL values in a column. insert overwrite avg_t values (2),(4),(6),(null),(null); -- The average of the above values is 4: (2+4+6) / 3. The 2 NULL values are ignored. select avg(x) from avg_t; -- Average only certain values from the column. select avg(x) from t1 where month = 'January' and year = '2013'; -- Apply a calculation to the value of the column before averaging. select avg(x/3) from t1; -- Apply a function to the value of the column before averaging. -- Here we are substituting a value of 0 for all NULLs in the column, -- so that those rows do factor into the return value. select avg(isnull(x,0)) from t1; -- Apply some number-returning function to a string column and average the results. -- If column s contains any NULLs, length(s) also returns NULL and those rows are ignored. select avg(length(s)) from t1; -- Can also be used in combination with DISTINCT and/or GROUP BY. -- Return more than one result. select month, year, avg(page_visits) from web_stats group by month, year; -- Filter the input to eliminate duplicates before performing the calculation. select avg(distinct x) from t1; -- Filter the output after performing the calculation. select avg(x) from t1 group by y having avg(x) between 1 and 20;
<< Aggregate Functions | COUNT Function >> | |