Skip to content

Instantly share code, notes, and snippets.

@calebhearth
Created March 15, 2017 17:58
Show Gist options
  • Save calebhearth/8a28fe70aa4bf4ff642098eb52539bdd to your computer and use it in GitHub Desktop.
Save calebhearth/8a28fe70aa4bf4ff642098eb52539bdd to your computer and use it in GitHub Desktop.
WITH my_surveys AS (
SELECT
surveys.*,
rank() OVER (ORDER BY surveys.created_at desc)
FROM surveys
JOIN comments
ON comments.ticket_id = surveys.ticket_id
WHERE user_cache IS NOT NULL
AND user_cache->>'email' = 'caleb@heroku.com'
)
SELECT
avg(s2.support_score * (s2.rank - s.rank) / 210.0) * 100 AS support_score,
s.comments,
'https://support.heroku.com/tickets/' || s.ticket_id::text AS url,
s.created_at
FROM my_surveys s
JOIN my_surveys s2
ON s2.rank >= s.rank
AND s2.rank < s.rank + 20
GROUP BY
s.comments,
s.ticket_id,
s.created_at
ORDER BY s.created_at desc
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment