Created
November 20, 2013 16:56
-
-
Save fredbenenson/7566727 to your computer and use it in GitHub Desktop.
Example Redshift Query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT * | |
FROM | |
(SELECT | |
month, | |
amount, | |
pledge_count, | |
SUM(1) OVER(PARTITION BY month ORDER BY pledge_count DESC ROWS UNBOUNDED PRECEDING) as row | |
FROM | |
(SELECT | |
TO_CHAR(CONVERT_TIMEZONE('UTC', 'America/New_York', backings.pledged_at), 'YYYY-MM-01') as month, | |
backings.amount as amount, | |
COUNT(DISTINCT backings.id) AS pledge_count | |
FROM | |
backings | |
GROUP BY month, backings.amount) AS backings_per_month | |
ORDER BY month) | |
WHERE row <= 10; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Goal of dplyr is to make it so you could express that in R with code that looks like this:
For redshift, dplyr could know that
rownum()
doesn't exist and it should use windowed sum instead.x %.% f(...)
is a shortcut forf(x, ...)
that makes it easier to read from left to right