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

1

6

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!

Gaylagayle answered 27/3, 2012 at 22:37 Comment(1)
I found an answer but I don't have enough reputation to answer my own question in 8 hours after posting. I will add my answer later tonight.Gaylagayle
I
13

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);
Inweave answered 27/3, 2012 at 23:14 Comment(1)
Exactly. That was what I found. Thanks anyway!Gaylagayle

© 2022 - 2024 — McMap. All rights reserved.