2016-04-19 8 views
1

私は、渡されたパラメータに基づいて、下のピボット(出力を参照)を生成するプロシージャを持っています。どのようにピボットされたプロシージャをSQLクエリとして呼び出すことができますか?

予想される出力に表示されるように、この新しい列のピボットされた値に100を加算するように、年の間に列を挿入できます。

ピボットprocをクエリとして呼び出す方法はありますか。そのため、選択クエリでこれらの計算を追加できますか?それとも簡単な方法がありますか?

create table t1 
(
    date int, 
    unita int, 
    unitb int, 
    unitc int 
) 

insert into t1 values (2010, 335, 52, 540) 
insert into t1 values (2011, 384, 70, 556) 
insert into t1 values (2012, 145, 54, 345) 


select * 
from 
(
    select date, value, unit 
    from 
    (
     select * 
     from t1 
    ) x 
    unpivot ([value] for unit in ([unita], [unitb], [unitc])) u 
) a 
pivot 
(
    sum(value) 
    for date in ([2010], [2011], [2012]) 
) p 

OUTPUT:

unit 2010 2011 2012 
---------------------- 
unita 335 384 145 
unitb 52 70 54 
unitc 540 556 345 

予想される出力:

unit 2010 2010a 2011 2011a 2012 
----------------------------------- 
unita 335 435 384 485 145 
unitb 52 150 70 170 54 
unitc 540 640 556 656 345 
+0

追加する必要がある列の数はどのくらいですか? – DhruvJoshi

答えて

1

私は、ピボットの結果に列を追加するには '簡単な' 方法が実際にそこだとは思いません。あなたは、その場合動的SQLなしで離れて得ることはできません。したがって、可能な解決策の1つは です。私はコメントにいくつかの説明を置いた。

DECLARE @dates TABLE ([date] varchar(4)) 
DECLARE @pivotColumns varchar(500) 
DECLARE @query nvarchar(max) 

-- First, you need to construct list of values you need to pivot - `[2010], [2010a], [2011], [2011a], [2012]`. 
SET @pivotColumns = '' 

INSERT INTO @dates 
SELECT DISTINCT [date] FROM t1 

SELECT 
    @pivotColumns = @pivotColumns + '[' + CAST([date] AS varchar(4)) + ']' + 
     CASE 
      WHEN [date] < (SELECT MAX([date]) FROM @dates) THEN + ',[' + CAST([date] AS varchar(4)) + 'a]' 
      ELSE '' 
     END + ',' 
FROM @dates ORDER BY [date] 

SET @pivotColumns = LEFT(@pivotColumns, LEN(@pivotColumns) - 1) 

-- Second - in the innermost query you need all the data for these columns in corresponding rows before unpivot. 
-- So we union main query with itself appending 'a' to 'date' values and 
-- incrementing values in unita, unitb, unitc columns by 100 for each row 
-- except for rows with the maximum 'date' value. 
-- Third - we use our @pivotColumns variable for pivot columns list. That's what 
-- this dynamic query is here for. 

SET @query = 
'select * 
from 
(
    select [date], value, unit 
    from 
    (
     select CAST(date AS varchar(5)) AS [date], unita, unitb, unitc from t1 
     union all 
     select CAST(date AS varchar(5)) + ''a'', unita + 100, unitb + 100, unitc + 100 from t1 
     WHERE [date] < (SELECT MAX([date]) FROM t1) 
    ) x 
    unpivot ([value] for unit in ([unita], [unitb], [unitc])) u 
) a 
pivot 
(
    sum(value) 
    for date in (' + @pivotColumns + ') 
) p 
' 
-- Execute the query. 
exec sp_executesql @query 
関連する問題