As The Question says that I'm Trying to insert some values from dynamic sql Query to TempTable using Pivot, so the ExistingCoulmnName
are not known to me
In the next step I want to perform some arithmatic operation lets say multipliation on those Column Values.
How Can I do that?
Attaching Some samples :
select @cols = STUFF((SELECT ',' + QUOTENAME(FYYear)
from #TempCapEx
group by FYYear
order by FYYear
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT CapitalExpenditureId,' + @cols + ' into
##GlobalTempForCapExPivotResult from
(
select CapitalExpenditureId, FYYear, indicatorvalue
from #TempCapEx
) x
pivot
(
sum(indicatorvalue)
for FYYear in (' + @cols + ')
) p'
execute (@query ) ;
So I have a ##GlobalTempForCapExPivotResult
as :
CapitalExpenditureId 2016-2017 2017-2018 2018-2019 2019-2020 2020-2021 2021-2022 2022-2023
22150 0.0000 35200.0000 35200.0000 35200.0000 35200.0000 35200.0000 NULL
32632 NULL 213695.0000 224379.0000 235599.0000 247379.0000 259748.0000 0.0000
1589 10252.0000 170370.0000 0.0000 0.0000 0.0000 0.0000 NULL
14988 0.0000 133000.0000 0.0000 0.0000 0.0000 0.0000 NULL
36877 NULL 303.0300 404.040 101.010 0.0000 0.0000 0.0000
So the Financial Year Columns may increase or decrease, so how can I do somthing like this :
Select [ExistingCoulmnName] * 3.5 from #GlobalTempForCapExPivotResult where [ExistingCoulmnName] = '[2016-2017]'
Expected Output:
CapitalExpenditureId 2016-2017 2017-2018 2018-2019 2019-2020 2020-2021 2021-2022 2022-2023
22150 0.00 * 3.5 35200.0000 * 3.5 35200.0000 * 3.5 35200.0000 * 3.5 35200.0000 * 3.5 35200.0000 * 3.5 NULL * 3.5