AVG_IF (Lakehouse v1)
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
Section titled “Analyze Syntax”func.avg_if(<column>, <cond>)Analyze Examples
Section titled “Analyze Examples”func.avg_if(table.salary, table.department=='IT').alias('avg_salary_it')
| avg_salary_it ||-----------------|| 65000.0 |SQL Syntax
Section titled “SQL Syntax”AVG_IF(<column>, <cond>)SQL Examples
Section titled “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_itFROM employees;Result
| avg_salary_it ||-----------------|| 65000.0 |