Perform Operations on Dynamic Columns from Pivot

南笙酒味 提交于 2019-12-08 11:56:23

问题


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

回答1:


There is no native way to do that inside a standard SQL statement. Instead you use some language (possibly even T-SQL) to write a new query in to a string, and then execute that new query. Much the same as your example already does.

DECLARE @field NVARCHAR(256) = N'2015-2016';

DECLARE @dynamicSelect NVARCHAR(MAX);

SET @dynamicSelect = N'SELECT *, 3.5 * [' + @field + N'] AS new_field FROM ##GlobalTempForCapExPivotResult';

EXECUTE(@dynamicSelect);

Also, note that this type of problem is WHY you shouldn't make that global temporary table in the first place. Normalised data structures are much friendlier to SQL. Do the formatting outside of SQL.

This is your query without the global temporary table...

SELECT
  CapitalExpenditureId,
  FYYear,
  indicatorvalue * 3.5
FROM
  #TempCapEx
GROUP BY
  CapitalExpenditureId,
  FYYear
ORDER BY
  CapitalExpenditureId,
  FYYear

You as a human being might prefer the flat file (excel style) table, but it is a really bad idea to make that type of table for use inside SQL. Stick to normalised data structures, and learn how to use them. Don't try to break SQL.




回答2:


You need to create a dynamic UPDATE statement:

DECLARE
    @UPDATEList NVARCHAR(MAX) = N''
    , @DynamicSQL NVARCHAR(MAX) = N'UPDATE ##GlobalTempForCapExPivotResult
SET ';

SELECT @UPDATEList += N'
    , '
    + QUOTENAME(FYYear) + N' = ' + QUOTENAME(FYYear) + N' * 3.5'
FROM #TempCapEx
GROUP BY FYYear;

SELECT @UPDATEList = SUBSTRING(@UPDATEList, 6, LEN(@UPDATEList) - 5);
SELECT @DynamicSQL += @UPDATEList + ';';

EXEC (@DynamicSQL);

that updates the global temp table.



来源:https://stackoverflow.com/questions/48078869/perform-operations-on-dynamic-columns-from-pivot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!