PeterTheGeo
PeterTheGeo

Reputation: 41

Turned pivot select query into a table

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

Upvotes: 0

Views: 2382

Answers (2)

Krish
Krish

Reputation: 39

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

Upvotes: 0

Raffaello.D.Huke
Raffaello.D.Huke

Reputation: 552

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 

Upvotes: 1

Related Questions