Thursday 15 March 2012

mysql - How to use GROUP BY and COUNT in SQL in this scenario -



mysql - How to use GROUP BY and COUNT in SQL in this scenario -

i working on voting system, there survey form, record info this

survey -------------- id q1 q2 q3

where q1 means question 1, possible value 1, 2 , 3, (means user select first , sec ..choice) while q2 means question 2 , possible value 1, 2, 3, 4, 5 ..etc

and have query in case, each question, count total number of each choice, , query result should e.g.

q1 , first selection , 10 q1 , second, 50 q1 , 3rd , 20...and on

i think of way there other elegant approach ? thanks

(select count(1) q1_third survey q1 = 3)...

one way case statements within count or sum() see below.

select sum(case when q1 = 1 1 else 0 end) q1_first, sum(case when q1 = 2 1 else 0 end) q1_second, sum(case when q1 = 3 1 else 0 end) q1_third survey

mysql sql database relational-database relationship

No comments:

Post a Comment