Take average of multiple nullable columns I have an example table (year) as below in PostgreSQL 9.5:

Name | 2010 | 2011 | 2012
-------------------------
A    |   10 |      |   40
B    |   10 |   20 |   30 


Now, if I write a simple query as shown below to take average for columns (2010, 2011, 2012) I will get the correct result for B but result for A will be NULL because of the NULL in the 2011 column:

select (2010+2011+2012)/3 as avg from year


Is there any way to write a query so that I can take average of only non-NULLs in a row?
The only correct and scalable solution to that problem is to normalize your model. 

However, you can normalize the data "on the fly" and then use standard SQL aggregation on the result. 

select name, avg(t.val)
from the_table
  cross join unnest (array["2010","2011","2012"]) with ordinality as t(val)
group by name;


Online example: https://rextester.com/TGTC30399

But I would strongly recommend to fix your data model by properly normalizing it.