I have this static version of a pivot
which gives this data
and then when it is dynamic it is those month-year columns that change and are set dynamically. Now, my challenge is that I need to supply two percentage calculations with the first being the % change between the second to last column and the last column (217% and 95% respectively (b-a)/a) and then I need to know the breakdown of the percentage XX and YY of the total (in this case 46% and 54% respectively). What I cannot see how to do is how do I programmatically choose the last column and perform calculations on either that column and the one before it or between the rows in that column?
Thank you for any help you can give me!
Willie
Code:
SELECT SBType,[2016-06],[2016-08],[2017-01] FROM
(
SELECT [MonthYear]
,Amount
,SBType
FROM [dbo].[vw_history]
WHERE SN=36521487
AND Amount>0
AND (Direction='Incoming' OR IsIncoming=1)
AND IsReturn=0
)src
PIVOT
(
SUM(Amount)
for MonthYear in ([2016-06],[2016-08],[2017-01])
) as pvt
which gives this data
Code:
SBType 2016-06 2016-08 2017-01
XX 91199725.1118 96441374.732 305296408.57
YY 147496746.94 186075209.717 362227931.77
and then when it is dynamic it is those month-year columns that change and are set dynamically. Now, my challenge is that I need to supply two percentage calculations with the first being the % change between the second to last column and the last column (217% and 95% respectively (b-a)/a) and then I need to know the breakdown of the percentage XX and YY of the total (in this case 46% and 54% respectively). What I cannot see how to do is how do I programmatically choose the last column and perform calculations on either that column and the one before it or between the rows in that column?
Thank you for any help you can give me!
Willie