mysql - Sum column values in flipped table with generated column names -


i have following mysql-code:

    select question,        sum(case when value = '1' 1 else 0 end) '1',       sum(case when value = '2' 1 else 0 end) '2',       sum(case when value = '3' 1 else 0 end) '3',       sum(case when value = '4' 1 else 0 end) '4',       sum(case when value = '5' 1 else 0 end) '5',       sum(case when value = '6' 1 else 0 end) '6',       sum(case when value = '7' 1 else 0 end) '7',       sum(case when value = '8' 1 else 0 end) '8',       sum(case when value = '9' 1 else 0 end) '9',       sum(case when value = '10' 1 else 0 end) '10',       count(value) total -- line should edited         (         select answer1 value, 'answer1' question         questionaire         union         select answer2 value, 'answer2' question         questionaire         union         select answer3 value, 'answer3' question         questionaire     ) src     group question 

this select statement flips table formatted this:

id   |  e-mail           | answer1 | answer2 | answer3 ------------------------------------------------------ 1    |  test@example.com |    1    |    6    |   8 2    |  test2@example.com|    1    |    1    |   7 3    |  test2@example.com|    1    |    1    |   0 

the flipped table looks this

question | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | total --------------------------------------------------------- answer 1 | 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0  |   3 answer 2 | 2 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0  |   3 answer 3 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0  |   3 

there total column counts values, want sum values columns before it. because when skips questions counts 0 , sum in total wrong (see table, answer 3). total 3, , should 2.

my question how sum values 1 10 within total column?

i write instead as:

sum(value in ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10') ) 

or, perhaps more as:

sum(value > '0') 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -