1
我有一个程序,根据传递给它的参数在下面生成枢轴(见输出)。我怎样才能调用一个pivoted过程作为一个sql查询?
我希望能够插入列之间的年份,因为我在预期的输出中显示,将添加100到这个新列中的pivoted值。
有没有办法调用一个数据透视表作为查询,以便我可以通过选择查询添加这些计算?还是有更简单的方法?
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
什么决定你需要添加多少列? – DhruvJoshi