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

need help with CASE function

P: 3
I have a table
Student Subject Score
Sally Math 98
Sally Reading 96
Billy Science 72
Billy Reading 63

I want a result
Student MathScore ReadingScore
Sally 98 96
Billy null 63

Select student,
case when subject=Math then Score else null end as MathScore,
case when subject=Reading then Score else null end as ReadingScore

My result
Student MathScore ReadingScore
Sally 98 null
Sally null 96
Billy null null
Billy null 63

Feb 10 '10 #1
Share this Question
Share on Google+
4 Replies

Expert 2.5K+
P: 2,878
Is there any error you're getting?

~~ CK
Feb 11 '10 #2

P: 3
No error, it just doesn't return the result I want. See example.
Feb 11 '10 #3

Expert 2.5K+
P: 2,878
Try to use PIVOT/UNPIVOT to accomplish your goal.

Happy Coding!!!

~~ CK
Feb 12 '10 #4

P: 3
Thanks for the tip, it's exactly what I need. Sadly, I'm not a sysadmin so I can't set the compatibility level to make the PIVOT function work. I guess it's just lip service when Microsoft says it wants to push access down to the "knowledge worker".

Anyway, thanks for your help!
Feb 12 '10 #5

Post your reply

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