Hive query to get sum of all the positive values and negative values of a column into two different columns

Assume that we have a table as below:

Column_name
1
-2
3
-4
5

Need to write a query to get sum of the positive values and negative values as below:

Positive_sum Negative_sum
9 -6

 

SELECT CASE WHEN column_name>0 THEN SUM(column_name) END AS positive_sum, CASE WHEN column_name<0 THEN SUM (column_name) END AS negative_sum FROM table;

 

Please comment if this is useful.

Leave a Reply

Your email address will not be published. Required fields are marked *