A user can vote on these numbers:
1, 2, 3, 4, 5
Then it's displayed as groups like this:
1 - 5 votes
2 - 3 votes
3 - 1 vote
4 - 17 votes
5 - 2 votes
Now I want the avarage rating score, like 3.9 or whatever it will be.
What I tried and failed:
Multiply all rows in the groups like:
1 - 1 * 5 votes
2 - 2 * 3 votes
3 - 3 * 1 vote
4 - 4 * 17 votes
5 - 5 * 2 votes
and then a sum of them where I get 52. Then I divide it with 15 which is the sum of all the valid scores. 52/15 = 3.46.
If I do that with the same votes on each, I can see how this is not correct:
1 - 1 * 1 votes
2 - 2 * 1 votes
3 - 3 * 1 vote
4 - 4 * 1 votes
5 - 5 * 1 votes
The result with my way would be 15/15 and that's 1 but it should be 3 in this case.
Any ideas?
I don't know where you pulled that $15$ denominator, but you're trying to do the weighted average. If you have a bunch of values $a_i$ to take the average from, all with a importance weight $w_i$, the weighted average is defined as follows : $$ \frac{ \sum_{i = 1}^n w_i \cdot a_i }{ \sum_{i = 1}^n w_i } $$
Note that the weights used in the numerator must be the same as the weights used in the denominator. In your problem the $a_i$ are the possible scores to give and the $w_i$ are the number of persons that gave that score. In your first case you get, $$ \frac{ 1\cdot5+2\cdot3+3\cdot1+4\cdot17+5\cdot2 }{ 5 + 3 + 1 + 17 + 2 } = \frac{92}{28} $$
In the second case, you get $$ \frac{ 1+2+3+4+5 }{ 1+1+1+1+1 } = \frac{15}{5} = 3 $$