I've got some simple survey data that I'd love to have a hand with. I work with PHP/MYSQL.
Basically, the survey has 20 questions with 3 possible answers for each: Y, N and N/A. I want to 'score' each survey at the end using the formula: Y/(Y+N).
The Question: How do I actually come up with the 'SCORE' column at the end? Using a SQL query or using PHP (somehow)? Is there some kind of COUNTIF
function in MYSQL like Excel has?
A query like: COUNTIF("Y", Column1:Column20) would be awesome—if it existed!
Suggestions appreciated! (I guess I'm looking for as much procedural advice as technical with this question).
Thanks
Terry
Q1 Q2 Q3 Q4 Q5 SCORE
---------------------
Person1 - y n y n n/a 50%
Person2 - y y y y y 100%
Person3 - y y y y 开发者_高级运维 n/a 100%
No tricks. A long winded SUM(CASE ...)
It'd be easier with one row per question... especially when you add more questions. See First Normal Form
SELECT
SUM(
CASE WHEN Q1 = 'y' THEN 1 ELSE 0 END +
CASE WHEN Q2 = 'y' THEN 1 ELSE 0 END +
...
CASE WHEN Q20 = 'y' THEN 1 ELSE 0 END
)
/
SUM(
CASE WHEN Q1 IN ('y', 'n') THEN 1 ELSE 0 END +
CASE WHEN Q2 IN ('y', 'n') THEN 1 ELSE 0 END +
...
CASE WHEN Q20 IN ('y', 'n') THEN 1 ELSE 0 END
),
Person
FROM
myTable
GROUP BY
Person
精彩评论