按月限制而不是所有结果

时间:2017-12-12 00:01:35

标签: sql hive

我知道要获得随机的1000条记录,我可以这样做:

select a, b, month, avg(c) as c
from
(select bla bla --string parsing and other data cleanup
from table) t
group by a, b, month
order by rand()
limit 1000

但是,这将总共提供1000条记录。

我想得到的是每月1000个随机记录(因为表只是大的方式)。如果有可能,你可以帮忙重写以上内容吗?

请注意,这是一个Hive问题。

提前致谢!

1 个答案:

答案 0 :(得分:2)

这是一种方法:

select t.*
from (select t.*,
             row_number() over (partition by month order by random()) as seqnum
      from t
     ) t
where sequm <= 1000;
相关问题