By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,379 Members | 1,380 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,379 IT Pros & Developers. It's quick & easy.

Pivot in SQL

P: 8
I have query that returns results like the following:

request_id field_id field_value
12345 2 abc
12345 3 asdf
12345 11 10
12346 2 def
12346 3 asdf
12346 11 20

and so on.
I would like to display the results like this:

request_id field_2 field_3 field_11
12345 abc asdf 10
12346 def asdf 20

I tried the floowing "pivot" command in SQL 2005:

select request_id, field_id, field_value
from some_table
pivot (
field_value for field_id in (2, 3, 11)
)

but I keep getting this error:

Error in FROM clause: near 'FOR'.
Unable to parse query text.

Does anyone have any idea what I am doing wrong?
Thanks in advance.
Apr 9 '08 #1
Share this Question
Share on Google+
1 Reply


ck9663
Expert 2.5K+
P: 2,878
I have query that returns results like the following:

request_id field_id field_value
12345 2 abc
12345 3 asdf
12345 11 10
12346 2 def
12346 3 asdf
12346 11 20

and so on.
I would like to display the results like this:

request_id field_2 field_3 field_11
12345 abc asdf 10
12346 def asdf 20

I tried the floowing "pivot" command in SQL 2005:

select request_id, field_id, field_value
from some_table
pivot (
field_value for field_id in (2, 3, 11)
)

but I keep getting this error:

Error in FROM clause: near 'FOR'.
Unable to parse query text.

Does anyone have any idea what I am doing wrong?
Thanks in advance.
Check the full syntax here

-- CK
Apr 10 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.