SQL 服务器:忽略 AVG NULL 值

SQL Server : ignore AVG NULL value

Select AVG(Salary) from Employee

输出:NULL

当我要将传入的值转换为整数或小数时,报错:

Input string was not in a correct format

AVG() 等聚合函数会忽略 null 值。所以你得到的结果表明:

  • 要么 salary 所有 行的 null table

  • 或者 table 根本没有行

如果你想在那种情况下 return 其他东西,比如 0,请使用 COALESCE():

Select COALESCE(AVG(Salary), 0) from Employee