Skip to content

Instantly share code, notes, and snippets.

@samflores
Created January 11, 2012 14:35
Show Gist options
  • Save samflores/1594936 to your computer and use it in GitHub Desktop.
Save samflores/1594936 to your computer and use it in GitHub Desktop.
-- Number of restaurants with 5, 4, 3, 3, 1 and 0 average ratings
SELECT stars_avg, count(stars_avg)
FROM (
SELECT FLOOR(AVG(r.stars)) AS stars_avg
FROM rates AS r
JOIN venues AS v ON v.id = r.rateable_id
JOIN types AS t ON t.id = v.type_id
WHERE t.name = "Restaurants"
AND r.rateable_type = "Venue"
GROUP BY v.id) AS star_avgs_tbl
GROUP BY stars_avg
ORDER BY stars_avg DESC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment