Hive 實現 分組後隨機取N條數據

hive取隨機的數據,可以使用rand()函數,用rand()對數據排序,取topN

如果要用到分組取隨機數,比如每個班級隨機取10人,針對這種每個分組取topN的情況,可以使用

row_number() over(partition by fieldx order by rand()) as rn
select id,
       class_id
from(
select id,
       class_id,
       row_number() over(partition by class_id order by rand()) as rn 
from table ) tb
where tb.rn <= 10

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章