如何按周限制多周?

时间:2015-10-01 02:10:17

标签: sql hive limit

在我的查询中,我试图每周带回前750个订单ID(按点击率)。

不是所有星期中的前750名。我无法弄清楚如何做到这一点。

select "topctrbyQ32015", weekofyear(day),order_id, (sum(clicks)/sum(impressions)) as ctr
from RDZ_TABLE rdz
where day >= '2015-07-06'
and day <= '2015-09-27'
group by "topctrbyQ32015", weekofyear(day),order_id
order by ctr desc
limit 750;

1 个答案:

答案 0 :(得分:0)

您需要使用窗口函数(在Hive 0.11及更高版本中可用):

select
    woy,
    order_id,
    ctr,
    rank() OVER (partition by woy order by ctr desc) as ctr_rank
from
    ( select 
        weekofyear(day) as woy,
        order_id, 
        (sum(clicks)/sum(impressions)) as ctr
    from 
        RDZ_TABLE rdz
    where 
        day >= '2015-07-06'
        and day <= '2015-09-27'
    group by 
        weekofyear(day),order_id
    ) z
where
    rank() OVER (partition by woy order by ctr desc) <= 750

有关详细信息,请参阅:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+WindowingAndAnalytics#LanguageManualWindowingAndAnalytics-PARTITIONBYwithonepartitioningcolumn,noORDERBYorwindowspecification