Skip to main content

Calculating aggregate values

You can group objects of a model by the specified grouping method and calculate summary statistics for each group. This feature is quite handy if you would like to get some high-level data about your model objects. As an example, if you keep ratings (1-5 stars) for a movie and your users rate the movies on a scale of 1 to 5 stars then at some point you might need to show summary statistics on the average rating of the movie and the number of ratings for each rating category (e.g., number of ratings for 1, 2, 3, 4 and 5-star ratings).

To calculate aggregate values, Altogic provides the Get Aggregate Results by Grouping node. The below service example gets user ratings from the database for the provided movie (passed from the endpoint's path as movieId) and calculates two statistics: the average rating and the rating count for each star rating category.

While calculating the average rating we first filter all the ratings and get the ratings associated with the movie with the specified identifier. After this filtering, we group all ratings and calculate the overall average rating of the movie.

When calculating the number of ratings for each category (1, 2, 3, 4, 5-stars) we group the ratings by the category first and calculate the count summary statistics on the grouped categories.