freeda
freeda

Reputation: 1

calculate row percentage in hive

I want to calculate percentage of each row to column total in total. Here it's my code

SELECT a, a/(SELECT SUM(a) FROM table b) AS percentage
FROM table b

it returns

cannot recognize input near 'SELECT' 'SUM' '(' in expression specification

https://cwiki.apache.org/confluence/display/Hive/Subqueries+in+SELECT subqueries It says subqueriesin complex expressions, aggregates, UDFs, etc. will not be supported by Hive.

How to fix it?

Upvotes: 0

Views: 731

Answers (1)

Vamsi Prabhala
Vamsi Prabhala

Reputation: 49260

Use sum analytic function. An empty OVER() clause sums up the values of a given column across all rows.

SELECT a, a/SUM(a) OVER() AS percentage
FROM table

Upvotes: 1

Related Questions