将透视选择查询转换为表格

时间:2015-12-29 05:03:48

标签: sql sql-server

我使用的是SQL Server 2014 Management Studio,我从网上调整了以下代码:

USE [DRILLHOLES_Export]
GO
     select * from
     (
        SELECT [DHSurveyId],[AttributeColumn],[AttributeValue]
              FROM mytest
     ) A
     PIVOT
     (
       Max(AttributeValue) 
          FOR [AttributeColumn] IN ([Azimuth],[AMG Azimuth],
                               [Dip])
     ) AS PVTTable

我想将它保存到表格中。谁能帮忙。 问候, 彼得

2 个答案:

答案 0 :(得分:0)

使用Create table或select into:

via:select into

     select * into talbe_name from
       ( SELECT [DHSurveyId],[AttributeColumn],[AttributeValue] FROM mytest) A 
          PIVOT(Max(AttributeValue) FOR [AttributeColumn] IN ([Azimuth],
                [AMG Azimuth], [Dip])) AS PVTTable

via:create table

   create table table_name (
     Azimuth varchar(10),
     AMG_Azimuth varchar(10),
     Dip varchar(10)
     );
   insert into table table_name(DHSurveyId,AttributeColumn,AttributeValue)
    select * from 
       ( SELECT [DHSurveyId],[AttributeColumn],[AttributeValue] FROM mytest) A 
          PIVOT(Max(AttributeValue) FOR [AttributeColumn] IN ([Azimuth],
                [AMG Azimuth], [Dip])) AS PVTTable 

答案 1 :(得分:0)

您可以使用INTO CALUSE(在关键字后输入新的表名)

 select * into yourtablename from
 (
    SELECT [DHSurveyId],[AttributeColumn],[AttributeValue] FROM mytest
 ) A
 PIVOT
 (
   Max(AttributeValue) 
      FOR [AttributeColumn] IN ([Azimuth],[AMG Azimuth],
                           [Dip])
 ) AS PVTTable