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?
avg()would also properly take care of NULL values. Adding columns like that is never a good idea. Modern hardware and databases can easily deal with much, much larger tables. – Aug 10 '19 at 15:04