Dear People,
Using SQL 2012, I am trying to create a pivot query where I do not know all of the column names in advance. My understanding is the only way to do this is to generate the query dynamically as a string and then execute the string.
That is fine, if that is what I have to do, but it feels wrong????
Question, can I avoid creating a dynamic query string for this? If so, how?
Please see following the queries used so far…
Regards
Greg
The query I have is:
USEmaster
Go
Alter DatabaseMatrixPivotQueryExampleSetSINGLE_USER WithRollbackImmediate
Go
Drop DatabaseMatrixPivotQueryExample
Go
CreateDatabaseMatrixPivotQueryExample
Go
USE MatrixPivotQueryExample
Go
CreateTableSalesTransaction(ProductType varchar(15) ,
SalesRegion varchar(15) ,
Amount decimal(18,2))
InsertInto SalesTransactionValues ( 'Car' , 'USA' , 2000)
InsertInto SalesTransactionValues ( 'Car' , 'Canada' , 3000 )
InsertInto SalesTransactionValues ( 'Car' , 'Australia', 4000 )
InsertInto SalesTransactionValues ( 'Truck' , 'USA' , 5000)
InsertInto SalesTransactionValues ( 'Truck' , 'Canada' , 6000 )
InsertInto SalesTransactionValues ( 'Truck' , 'Australia', 7000 )
InsertInto SalesTransactionValues ( 'Aircraft','USA' , 8000 )
InsertInto SalesTransactionValues ( 'Aircraft','Canada' , 9000 )
Go
WithTempTableNameAs
(Select SalesRegion ,
ProductType ,
Sum(Amount)AsAmount
From SalesTransaction
GroupbySalesRegion ,
ProductType )
Select SalesRegion ,
IsNull([Car] ,0) asCar ,
IsNull([Truck] ,0) asTruck ,
IsNull([Aircraft],0)asAircraft
From TempTableName
pivot (sum(Amount)forProductType in([Car],[Truck],[Aircraft]))SomeNameThatIsNotUsed
For this example, the query works fine giving:
SalesRegion Car Truck Aircraft
------------ --------- -------- --------
Australia 4000.00 7000.00 0.00
Canada 3000.00 6000.00 9000.00
USA 2000.00 5000.00 8000.00
If I add a new SalesRegion, that works fine, just adding another row to the result:
insertinto SalesTransactionvalues ( 'Car' , 'New Zealand', 100 )
insertinto SalesTransactionvalues ( 'Truck' , 'New Zealand', 200 )
insertinto SalesTransactionvalues ( 'Aircraft','New Zealand', 300)
Go
SalesRegion Car Truck Aircraft
--------------- -------- ---------- --------
Australia 4000.00 7000.00 0.00
Canada 3000.00 6000.00 9000.00
New Zealand 100.00 200.00 300.00
USA 2000.00 5000.00 8000.00
BUT, if I add a new product type (column), I do not get the result without adjusting the query...
insertinto SalesTransactionvalues ( 'Bus','USA' , 100 )
insertinto SalesTransactionvalues ( 'Bus','Canada' , 200 )
insertinto SalesTransactionvalues ( 'Bus','Australia' , 300 )
insertinto SalesTransactionvalues ( 'Bus','New Zealand', 400)
Go
WithTempTableNameAs
(Select SalesRegion ,
ProductType ,
Sum(Amount)AsAmount
From SalesTransaction
GroupbySalesRegion ,
ProductType )
Select SalesRegion ,
IsNull([Car] ,0) asCar ,
IsNull([Truck] ,0) asTruck ,
IsNull([Aircraft],0)asAircraft ,
IsNull([Bus] ,0) asBus
From TempTableName
pivot (sum(Amount)forProductType in([Car],[Truck],[Aircraft],[Bus]))SomeNameThatIsNotUsed
SalesRegion Car Truck Aircraft Bus
--------------- -------- -------- -------- -------
Australia 4000.00 7000.00 0.00 300.00
Canada 3000.00 6000.00 9000.00 200.00
New Zealand 100.00 200.00 300.00 400.00
USA 2000.00 5000.00 8000.00 100.00
(4 row(s) affected)
Pi