title: MySQL average of non-null columns
body: <p>I've kludged my way through by doing this, but I feel there's probably a cleaner/faster/better solution, and am always keen to learn:</p>
  
  <pre><code>UPDATE myTable SET fAvg =
  
  (COALESCE(f1,0) + COALESCE(f2,0) + COALESCE(f3,0) + COALESCE(f4,0))
  
  /
  
  (
  CASE WHEN f1 IS NOT NULL THEN 1 ELSE 0 END + 
  CASE WHEN f2 IS NOT NULL THEN 1 ELSE 0 END + 
  CASE WHEN f3 IS NOT NULL THEN 1 ELSE 0 END +
  CASE WHEN f4 IS NOT NULL THEN 1 ELSE 0 END 
  )
  </code></pre>
  
  <p>So, how can that be improved?</p>