Last active
August 29, 2015 14:09
-
-
Save elliottcordo/075537cbb5ce4f78acc7 to your computer and use it in GitHub Desktop.
redshift ntile query
This file contains hidden or 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
drop table zzt; | |
create temporary table zzt as | |
with n_tile as | |
( | |
select case when cnt>5 then 5 else cnt end as cnt | |
from | |
( select count(1)/50 as cnt | |
from temp.godaddy_viewing_summary_daily_visit) a | |
) | |
select *, count(1) over (partition by event_type, n_tile) as group_cnt | |
from ( | |
select * , ntile(cnt) over (partition by event_type order by total_exposures desc) as n_tile | |
from temp.zzzz | |
cross join n_tile | |
where total_exposures>0 ) a; | |
select group_cnt,event_type, n_tile, count(1), max(total_exposures), min(total_exposures), avg(total_exposures) from zzt group by n_tile, event_type, group_cnt | |
order by 1,2 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment