问题
I have a table, colors
in Hive that looks like this:
id cname
1 Blue
2 Green
3 Green
4 Blue
5 Blue
I need help with writing a Hive query that gives the percentages of each color in the cname column. Something that looks like this:
Blue 60%
Green 40%
Thanks in advance!
回答1:
Using analytics functions:
select cname, concat(pct, ' %') pct
from
(
select (
count(*) over (partition by cname)/
count(*) over ()
)*100 as pct,
cname
from (--Replace this subquery with your table
select stack (5,
1, 'Blue',
2, 'Green',
3, 'Green',
4, 'Blue',
5, 'Blue' ) as (id, cname)
) colors
)s
group by cname, pct;
Result:
OK
Blue 60.0 %
Green 40.0 %
Just replace colors
subquery with your table
来源:https://stackoverflow.com/questions/52467496/calculate-the-percentage-of-categories-in-a-column-in-hive