Skip to content

Instantly share code, notes, and snippets.

@elliottcordo
Last active August 29, 2015 14:09
Show Gist options
  • Save elliottcordo/075537cbb5ce4f78acc7 to your computer and use it in GitHub Desktop.
Save elliottcordo/075537cbb5ce4f78acc7 to your computer and use it in GitHub Desktop.
redshift ntile query
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