是否可以从unpivot结果创建一个新表?

时间:2018-03-13 11:38:48

标签: sql sql-server tsql unpivot

我想从一个unpivot的结果创建一个新表,是否可以使用unpivot的结果来创建一个新表? 例如,您可以添加类似Insert into newtable

的内容
Begin tRAN
select 2 AS Category_ID,
    Techcentre,
        kpi,
      Quarter,
      Result
From [Technology_Centre_New_Data_Import]
UNPIVOT
(
Result
for Quarter IN (Q1_15,Q2_15,Q3_15,Q4_15,Q1_16,Q2_16,Q3_16,Q4_16,Q1_17,Q2_17,Q3_17,Q4_17,Target_15,Target_16,Target_17)
)unpvt;

2 个答案:

答案 0 :(得分:0)

是的,INSERT INTO ... SELECT语法应该在这里工作:

INSERT INTO yourTable (co1, col2, col3, col4, col5)
SELECT 2 AS Category_ID, Techcentre, kpi, Quarter, Result
FROM [Technology_Centre_New_Data_Import]
UNPIVOT
(
    Result FOR Quarter IN (Q1_15,Q2_15,Q3_15,Q4_15,Q1_16,Q2_16,Q3_16,Q4_16,Q1_17,Q2_17,Q3_17,Q4_17,Target_15,Target_16,Target_17)
) unpvt;

答案 1 :(得分:0)

只需使用select into

select 2 AS Category_ID, Techcentre, kpi, Quarter, Result
into newTable
From [Technology_Centre_New_Data_Import]
unpivot (Result for Quarter in (Q1_15, Q2_15, Q3_15, Q4_15, Q1_16, Q2_16, Q3_16, Q4_16, Q1_17, Q2_17, Q3_17, Q4_17, Target_15, Target_16, Target_17)
        ) unpvt;