Choose a Random Row as Aggregate Function in Hive

Choose a Random Row as Aggregate Function in Hive

I want to group by a column and then select random rows from another column. In Presto, there's arbitrary.

E.g. my query is:

SELECT a, arbitrary(b)
FROM foo
GROUP BY a

How do I do this in Hive?

Edit:

By "random", I meant "arbitrary". It could just be the first row every time.

5

1 Answer

You can use the below logic to get your required result in Hive. Provide a row_number to rand(b) and choose any row_number you want. Every time it will return a random value from column b.

select a, b
from (
select a, b,row_number() over( partition by a order by rand(b) asc) rn from foo
)a
where rn=1
group by a, b;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins
Author

Sarah Jenkins

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.