We help IT Professionals succeed at work.

pivot() function

1,830 Views
Last Modified: 2011-11-18
What's wrong with this query?

select Type, MonthNo, amount from MyTable
pivot (
              sum(amount)
              for MonthNo
              in (3, 4, 5, 6)
) as p

it returns error: Incorrect syntax near '3'.
I tried putting it also on quotation marks as :  in ('3', '4', '5', '6'). but same error :(

Type is in varchar
MonthNo is an integer (values are 3, 4, 5, and 6)
Amount is of money data type



Comment
Watch Question

Commented:
try

select Type, MonthNo, amount from MyTable
pivot (
              sum(amount)
              for MonthNo
              in ([3],[4], [5], [6])
) as p

Commented:
or

select [3],[4], [5], [6] from MyTable
pivot (
              sum(amount)
              for MonthNo
              in ([3],[4], [5], [6])
) as p

Commented:
SELECT
    Type,
    MonthNo,
    AmountMarch = ISNULL([3],0),
    AmountApril = ISNULL([4],0),
    AmountMay = ISNULL([5], 0),
    AmountJune = ISNULL([6],0)
FROM
    MyTable
PIVOT (
    SUM(amount)
    FOR MonthNo IN ([3].[4],[5],[6])
) AS p
 
Faiga DiegelSr Database Engineer
CERTIFIED EXPERT

Author

Commented:
Appari,

I used your second suggestion and it worked. However, i think I didnt get the correct result. It should be the sum of all Month No per type, right?

Type               3                     4                   5                   6
car          0.00      NULL      NULL      NULL
car           NULL      NULL      30.00      NULL
car           NULL      NULL      -0.52      NULL
car           NULL      NULL      -0.22      NULL
house           NULL      NULL      0.15      NULL
house          NULL      NULL      0.15      NULL
house           NULL      NULL      3.97      NULL
house           NULL      NULL      6.96      NULL
house           NULL      NULL      25.00      NULL
car           NULL      14.63      NULL      NULL
car           NULL      -1.24      NULL      NULL
car           NULL      1.00      NULL      NULL
car           NULL      -0.15      NULL      NULL
house           NULL      0.10      NULL      NULL
house           NULL      1.00      NULL      NULL
house           NULL      1.08      NULL      NULL
house           NULL      1.20      NULL      NULL
house           NULL      -1.24      NULL      NULL
house           NULL      1.50      NULL      NULL
house           NULL      -14.63      NULL      NULL

ptjcb, it ried your suggestion but i got this error:
"Prefixed columns are not allowed in the column list of a PIVOT operator."
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Faiga DiegelSr Database Engineer
CERTIFIED EXPERT

Author

Commented:
Thanks appari... it worked with some modifications for NULL values. I took ptjcb idea of ISNULL command so I have to split points :)

thanks both of you! great help!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.