Coding exercise: Get average value from entity

As a coding exercise I build an application.
This application contains surveys where a user can answer 1 to 5 in response. Now to show the results, I'm doing some works with graphs. And in this particular graph I want to show the average value for a survey.

Now to determine the average some coding has been done in the preparation (of the graph data).

I found a solution to calculate the average value (which I included as attachment). And since one can always learn, I'm looking for feedback on my solution. Since I'm bearing it all here, please be kind with me ;)

Some notes: all entities were switched to static entities to have them prefilled with values.

So here is my logic:

So happy to get feedback on my solution to get the average value, if there were other / easier ways to get the same result.
See attachment for full solution.



Hi Paul,

Normally you would be able to do this just in aggregate. You need to have the survey inner join with the scores. After that group by the survey, sum the score column and count the score id column. The average would be the score divided by the count. With this you have a list of all the surveys with the respective averages.



Thank you for looking into this.
Turned out to be simpler than I thought. Which is a good thing, because I learned something from your solution :)

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.