AVG在同一列上由另一列中的值单个sql查询

I want to find AVG on same column by values in different column single sql query.

table - question_rating
review_id
question_id
rating
  • for each review there are multiple 16 questions and ratings.
  • question_id values are 1 to 16.
  • rating values are from 1 to 5.

I want to find avg of group of questions as

Desired output-

qustion_id     rating
-------------------------
g1               4.4
g2               3.7
g3               5.6

g1 is group of questions (1,3) g2 is group of questions (2,6) g3 is group of questions (7,8) ....g8

pseudo code-

 select (if(qustion=1 and question=3) as g1,
if(qustion=2 and question=6) as g2), ..
)avg(rating of respective group) from question_rating

I know it can be done by taking query separately but I want to know by single query.

Or any easy way to find such output by php,etc.

You should make a table question_groups

question_id  group_id
q1          g1
q2          g2
q3          g1
q4          g3
q5          g3
q6          g2

And the select will be

select group_id, avg(rating)
from question_rating a
  join question_groups b on (a.question_id= b.question_id)
group by group_id
SELECT
  CASE question_id 
  WHEN 1 THEN 'g1'
  WHEN 3 THEN 'g1'
  WHEN 2 THEN 'g2'
  WHEN 4 THEN 'g2'
  ELSE 'others' END AS qGroup,
  AVG(rating) AS AvgRating
FROM question_rating
GROUP BY 
  CASE question_id 
  WHEN 1 THEN 'g1'
  WHEN 3 THEN 'g1'
  WHEN 2 THEN 'g2'
  WHEN 4 THEN 'g2'
  ELSE 'others' END