Skip to content

Instantly share code, notes, and snippets.

@calebhearth
Created March 15, 2017 17:40
Show Gist options
  • Save calebhearth/c9498c151e41dc7c6a164efb873a1932 to your computer and use it in GitHub Desktop.
Save calebhearth/c9498c151e41dc7c6a164efb873a1932 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'
), csat AS (
SELECT
avg(support_score) * 100 AS support_score,
'Overall' AS comments,
'' AS url,
now() as created_at
FROM my_surveys
UNION
SELECT
avg(s2.support_score) 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
GROUP BY s.comments, s.ticket_id, s.created_at
)
SELECT
support_score,
comments,
url
FROM csat
ORDER BY created_at desc;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment