Skip to content

Instantly share code, notes, and snippets.

@iAmWillShepherd
Created November 7, 2016 02:16
Show Gist options
  • Save iAmWillShepherd/2515f9f8b97ae2a31fdf79a1f7e6c16f to your computer and use it in GitHub Desktop.
Save iAmWillShepherd/2515f9f8b97ae2a31fdf79a1f7e6c16f to your computer and use it in GitHub Desktop.
SELECT
data.name
, data.stars AS avgStars
, data.numberOfReviews
, (cool / totalVotes) AS `% cool`
, (useful / totalVotes) AS `% useful`
, (funny / totalVotes) AS `% funny`
FROM (
SELECT
b.business_id
, b.stars
, b.name
, count(r.business_id) AS numberOfReviews
, sum(r.votes_cool) AS cool
, sum(r.votes_useful) AS useful
, sum(r.votes_funny) AS funny
, sum(r.votes_cool + r.votes_funny + r.votes_useful) AS totalVotes
FROM business AS b
INNER JOIN review AS r ON
b.business_id = r.business_id
GROUP BY b.business_id) AS data
ORDER BY numberOfReviews DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment