Reputation: 632
Table:
col1 2018 2017 2016 2015 2014 2013 2012 2011
abc 8721 1273.2 122 (null) (null) (null) (null) (null)
def (null) 654.67 (null) (null) 0 67 9.7 876
ghi (null) (null) (null) (null) (null) (null) (null) (null)
jkl 0 124 875 100 761 26.1 239.1 2987
I want to calculate difference between years, where output would be like:
col1 2018-2017 2017-2016 2016-2015 2015-2014 2014-2013 2013-2012 2012-2011
abc 7447.8 1151.2 122 0 0 0 0
def -654.67 654.67 0 0 -67 57.3 -866.3
ghi 0 0 0 0 0 0 0
jkl -124 -751 775 -661 734.9 -213 -2747.9
For example, between 2018 and 2017 for row1 is 8721-1273.2 = 7447.8
Upvotes: 1
Views: 53
Reputation: 164064
You can subtract the columns like this:
select col1,
coalesce(`2018`, 0) - coalesce(`2017`, 0) as `2018-2017`,
coalesce(`2017`, 0) - coalesce(`2016`, 0) as `2017-2016`,
........................................................
from tablename
Upvotes: 1