Say I have a table like this
ID -- Val1 --- Val2
1     4        5
1     7        2
1     5        9
2     9        8
2     1        2
3     9        8
3     9        5
3     7        4
3     5        7
4     7        3
5     7        9
5     4        4

How do I get the averages of Val1 and Val2 per unit ID and store it into a new table? Notice that the number of rows per ID is different.

Eg new table:
ID -- Av_Val1 -- Av_Val2
1      5          5
2      5          5

and so on..

Recommended Answers

All 2 Replies

You can use something like:

INSERT INTO `new_table` (`av_val1`, `av_val2`)
    SELECT AVG(`val1`) AS `av_val1`, AVG(`val2`) AS `av_val2`
    FROM `table` GROUP BY `id`

Thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.