Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compute sum of a field in all the rows from an alias

What I want to do is to sum values of a field in all rows in an alias. This must be simple but somehow I can't find the answer. This is probably because what I want is a scalar value while PIG handles datasets? I guess I can create a row with a field which is the sum? Please advise!

like image 256
kee Avatar asked Mar 27 '12 22:03

kee


1 Answers

This can be achieved using a GROUP ALL to bring everything into a single group, and then the SUM function to add together all the fields:

DESCRIBE a
a: (name, age, height)

b = GROUP a ALL;
c = FOREACH b GENERATE SUM(a.age);
like image 109
Chris White Avatar answered Nov 15 '22 09:11

Chris White