0

I'm using SQL Server 2014 Management Studio, I adapted the following code from a net:

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

I want to save it to a table. Can anyone help. Regards, Peter

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Possible duplicate of [How to create a table from select query result in SQL Server 2008](http://stackoverflow.com/questions/16683758/how-to-create-a-table-from-select-query-result-in-sql-server-2008) – Tab Alleman Dec 29 '15 at 15:46

2 Answers2

1

use Create table or 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 
0

YOU CAN USE INTO CALUSE (After into keyword your new tablename)

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