sql server - Adding a total column PIVOT TABLE -


here code guys want able return total column of 2016,2015,2014,2013 when

sum([2016]+[2015]+[2014]+[2013]) revenue returns nulls.

or sum(2016+2015+2014+2013) revenue returns different number if go take calculator , add numbers in 2013 2014 2015 , 2016 numbers doesn't add in total column.

 select        aircarriername,          sum([2016]) [2016],          sum([2015]) [2015],          sum([2014]) [2014],          sum([2013]) [2013]                     sum_orders            pivot         (          sum(sum_orders.sum_sellprice)                 sum_orders.orderperiodyear in ([2016],[2015],[2014],[2013])             )as pvt               orderstatus in ('invoiced','open') , aircarriername      not 'null'      , aircarriername = 'cathay pacific airways ltd.'              group aircarriername               order [2016] desc 

this code returns following enter image description here

when add lines creating total column get, instead of having nice sum of 2013 2016 in total column

sum([2016]+[2015]+[2014]+[2013]) total 

enter image description here

this query total column included returning nulls

select        aircarriername,  sum(sum_buyprice) buy_price,  sum(sum_sellermargin) margin, sum(sum_grossweightkg/1000)as kilos_total,                     sum([2016]) [2016],  sum([2015]) [2015], sum([2014]) [2014], sum([2013])                           [2013],                      sum([2016]+[2015]+[2014]+[2013])as [total]             sum_orders    pivot (  sum(sum_orders.sum_sellprice)         sum_orders.orderperiodyear in ([2016],[2015],[2014],[2013])     )as pvt       orderstatus in ('invoiced','open') , aircarriername not 'null'      group aircarriername 


Comments