AVG_IF
The suffix -If can be appended to the name of any aggregate function. In this case, the aggregate function accepts an extra argument – a condition.
Analyze Syntax
func.avg_if(<column>, <cond>)
Analyze Examples
func.avg_if(table.salary, table.department=='IT').alias('avg_salary_it')
| avg_salary_it |
|-----------------|
| 65000.0 |
SQL Syntax
AVG_IF(<column>, <cond>)
SQL Examples
Create a Table and Insert Sample Data
CREATE TABLE employees (
id INT,
salary INT,
department VARCHAR
);
INSERT INTO employees (id, salary, department)
VALUES (1, 50000, 'HR'),
(2, 60000, 'IT'),
(3, 55000, 'HR'),
(4, 70000, 'IT'),
(5, 65000, 'IT');
Query Demo: Calculate Average Salary for IT Department
SELECT AVG_IF(salary, department = 'IT') AS avg_salary_it
FROM employees;
Result
| avg_salary_it |
|-----------------|
| 65000.0 |
Last modified June 11, 2024 at 8:46 PM EST: adding aggregate functions (68e518e)