ster ster - 1 month ago 7
MySQL Question

Average value form column - mySQL

I have this table:

+----+---------+------+----------+
| ID | ID_USER | NOTE | QUESTION |
+----+---------+------+----------+
| 1 | 12 | 3 | 1 |
| 2 | 31 | 2 | 1 |
| 3 | 12 | 9 | 1 |
| 4 | 31 | 8 | 2 |
| 5 | 12 | 10 | 2 |
| 6 | 31 | 3 | 2 |
+----+---------+------+----------+


How do I export values to group them by ID_USER, QUESTION and making an average of notes? The values to be grouped as in the table below:

+----+----------+---------+-------------+
| ID | QUESTION | ID_USER | MEDIUM_NOTE |
+----+----------+---------+-------------+
| 1 | 1 | 12 | 6 |
| 2 | 1 | 31 | 2 |
| 3 | 2 | 12 | 10 |
| 4 | 2 | 31 | 5.5 |
+----+----------+---------+-------------+


Thank you!

Answer
SELECT 
    id_user,
    question,
    AVG(note) AS medium_note
FROM notes 
GROUP BY id_user, question

You can group by user and question, then calculate the average.

Example