SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL PIVOT problem


SQL PIVOT problem

Author
Message
Davebhoy
Davebhoy
Valued Member
Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)

Group: General Forum Members
Points: 68 Visits: 44
Hi

I'm getting the error message 'Incorrect syntax near PIVOT' when I try the following code as a view

SELECT year_, client, job, [Payroll] AS 'payr_perc', [Operational] AS 'oper_perc', [Administration] AS 'admin_perc', [Establishment] AS 'estab_perc',
[Financial] AS 'finan_perc', [Miscellaneous] AS 'misc_perc'
FROM (SELECT *
FROM dbo.budprepv_ExpAlloc_perc) ps PIVOT(SUM(Alloc_perc) FOR ExpHead IN ([Payroll], [Operational], [Administration], [Establishment],
[Financial], [Miscellaneous])) AS pvt

I can't see what's wronf! Could someone help me please?

Thanks

Dave
Davebhoy
Davebhoy
Valued Member
Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)

Group: General Forum Members
Points: 68 Visits: 44
Is PIVOT not recognised in SQL 2000? Maybe its only, SQL 2005.....
Terry Troisi
Terry Troisi
SSC Rookie
SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)

Group: General Forum Members
Points: 36 Visits: 143
That's correct. Pivot was introduced in SQL 2005.

You can use CASE to pivot in SQL 2000 (or all other versions for that matter). Here's an example:

http://social.msdn.microsoft.com/Forums/sqlserver/en-US/951ad7d4-d21e-4a13-9a5b-5d156fe3d90d/aggregated-case-expressions-versus-the-pivot-operator-is-one-better-than-the-other
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16200 Visits: 19059
Check out the following links
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Davebhoy
Davebhoy
Valued Member
Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)

Group: General Forum Members
Points: 68 Visits: 44
Thanks again.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search