dimanche 14 février 2016

Getting median of column values with SQLite

I have a table containing user_id, movie_id, rating. These are all INT, and ratings range from 1-5.

I want to get the median rating and group it by user_id, but I'm having some trouble doing this.

My code at the moment is:

SELECT AVG(rating)
FROM (SELECT rating
      FROM movie_data
      ORDER BY rating
      LIMIT 2 - (SELECT COUNT(*) FROM movie_data) % 2
      OFFSET (SELECT (COUNT(*) - 1) / 2
              FROM movie_data));

However, this seems to return the median value of all the ratings. How can I group this by user_id, so I can see the median rating per user?

Any help is appreciated! Thanks.

Aucun commentaire:

Enregistrer un commentaire