Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Trying to Create Pivot Table

Author  Topic 

bconner
Starting Member

48 Posts

Posted - 2010-05-24 : 12:02:01
I am trying to create a Pivot table that will put the Years as the columns and use Price_Per_Unit as the aggregate. Below is the code I am using but I keep getting the following error

Msg 102, Level 15, State 1, Line 26
Incorrect syntax near '2009'.








SELECT
*
FROM

(
SELECT [DIVISION]
,[CPT]
,QTY
,CHG
,(CHG/QTY) AS PRICE_PER_UNIT
,[LOC]
,[NAME]
,[PRIFSC]
,[MOD1]
,[REF_LOC]
,[GRP]
,[CHARGE_TYPE]
,[MONTH]
,[YEAR]
FROM [AdHoc].[dbo].[Charges_Per_Unit_2009_2010]
) P
PIVOT
(
SUM(PRICE_PER_UNIT)
FOR YEAR IN (2009,2010)
)
AS PVT



Brian

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-05-24 : 12:07:36
Shot in the dark...
Try changing it to
(
SUM(PRICE_PER_UNIT)
FOR YEAR IN ([2009],[2010])
)
Go to Top of Page

bconner
Starting Member

48 Posts

Posted - 2010-05-24 : 12:31:41
That did the trick! Thanks vijayisonly

Brian
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-05-24 : 13:14:43
Np.
Go to Top of Page

zstarsales04
Starting Member

20 Posts

Posted - 2010-05-25 : 02:36:08
spam removed
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-05-25 : 05:04:09
For unknown number of years, use dynamic pivot
http://beyondrelational.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -