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
 Calculating Survey Scores

Author  Topic 

a2l2en
Starting Member

11 Posts

Posted - 2007-10-16 : 19:08:57
I have surveys that I need to add weights to and was wondering if there was a way to convert the contents of a column.

select empid, ans_for_ql,
(if ans_for_ql = A then 0, B then 3, C then 5) as weightscore,
ans_for_q2,
(if ans_for_q2 = A then 8, B then 4, C then 2, D then 1, E then 0) as weightscore

Here is what the table looks like:

empid | Ans_for_Q1 | Ans_for_Q2
1001 A C
1002 B E

And these are the possible answers and what they need to be converted to:

Weights for answers to Question1
Q1_A = 0
Q1_B = 3
Q1_C = 5

Weights for answers to Question2
Q2_A = 8
Q2_B = 4
Q2_C = 2
Q2_D = 1
Q2_E = 0

Kristen
Test

22859 Posts

Posted - 2007-10-17 : 00:43:59
(if ans_for_ql = A then 0, B then 3, C then 5) as weightscore

needs to be something like:

CASE ans_for_ql WHEN 'A' then 0 WHEN 'B' then 3 WHEN 'C' then 5 END as weightscore

Kristen
Go to Top of Page

a2l2en
Starting Member

11 Posts

Posted - 2007-10-17 : 11:16:33
The following case worked:

SELECT empid,
Score1 = 0.05, ms.question1 as Answer_1,
CASE WHEN ms.question1 = 'a' THEN 8
WHEN ms.question1 = 'b' THEN 5
WHEN ms.question1 = 'c' THEN 2
else 'error'
END as weightscore
from table
where ...

Thanks Kristen!!
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-10-17 : 12:21:58
be careful that you are mixing numbers and strings in the CASE statement (8, 5, 2, "error"). SQL either won;t like the "error" if it springs up, or will treat all the others as Strings - which may have unexpected side effects.

NULL is a good choice for "other" - so long as you catch it later on - as it is happy being any datatype
Go to Top of Page
   

- Advertisement -