Skip to content

Instantly share code, notes, and snippets.

@mraaroncruz
Created April 2, 2013 08:29
Show Gist options
  • Save mraaroncruz/5290766 to your computer and use it in GitHub Desktop.
Save mraaroncruz/5290766 to your computer and use it in GitHub Desktop.
Hacker News algorithm in SQL
SELECT x.*
FROM POSTS x
JOIN (SELECT p.postid,
SUM(v.vote) AS points
FROM POSTS p
JOIN VOTES v ON v.postid = p.postid
GROUP BY p.postid) y ON y.postid = x.postid
ORDER BY (y.points - 1)/POW((((UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(x.timestamp))/3600)+2, 1.5) DESC
LIMIT n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment