sql多列求平均值

mysql

SELECT   *,CASE WHEN SUM(IFNULL(emp_no_new,0)+IFNULL(emp_no_new_1,0)) =0 THEN 0 
 ELSE 
SUM(IFNULL(emp_no_new,0)+IFNULL(emp_no_new_1,0))/ 
SUM(IF(emp_no_new IS NULL,0,1)+IF(emp_no_new_1 IS NULL,0,1)) END AS avg 
FROM dept_emp GROUP BY emp_no

oracle

select case when sum(nvl(CGYQSY1,0)+nvl(CGYQSY2,0)+nvl(CGYQSY3,0))=0 then 0
 else 
 sum(nvl(CGYQSY1,0)+nvl(CGYQSY2,0)+nvl(CGYQSY3,0))/
 sum(decode(CGYQSY1,null,0,1)+decode(CGYQSY2,null,0,1)+decode(CGYQSY3,null,0,1))
 from a group by id

你可能感兴趣的:(SQL,sql,数据库)