Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]

Archives of Support Questions (Read Only)

This is an archived board for historical reference. Information and links may no longer be available or relevant
Announcements
This board is archived and read-only for historical reference. To ask a new question, please post a new topic on the appropriate active board.

Hive - i would like to calculate percentage of column and based on the percentage i would like to load the data into another table

avatar
Expert Contributor

Hive - i would like to calculate percentage of column and based on the percentage i would like to load the data into another table(if the percentage of n is less 20%) or else not to load

colA

y

y

y

n

------------------ Output: -- This is what i am expecting

y 80%

n 20%

1 ACCEPTED SOLUTION

avatar

@PPR Reddy

Here goes the solution (You can do it in other ways if you choose to):

Column name in table a is c

hive> select * from a;

OK

y

y

y

n

hive>

Query:

hive> select c,per from(  > select x.c c,(x.cc/y.ct)*100 per,(z.cn/y.ct)*100 pern  from  > (select c, count(*) cc from a group by c) x, (select count(*)ct from a) y,  > (select c, count(*) cn from a where c='n' group by c) z) f  > where pern > 20;

Output:

OK

n 25.0

y 75.0

Thanks

View solution in original post

1 REPLY 1

avatar

@PPR Reddy

Here goes the solution (You can do it in other ways if you choose to):

Column name in table a is c

hive> select * from a;

OK

y

y

y

n

hive>

Query:

hive> select c,per from(  > select x.c c,(x.cc/y.ct)*100 per,(z.cn/y.ct)*100 pern  from  > (select c, count(*) cc from a group by c) x, (select count(*)ct from a) y,  > (select c, count(*) cn from a where c='n' group by c) z) f  > where pern > 20;

Output:

OK

n 25.0

y 75.0

Thanks