Ricardo
Ricardo

Reputation: 11

Calculating MIN, AVG, MAX based on condition

I have a relation from a piracy dataset with the following fields:

date, country_code, torrent_id, first_seen, torrent_size, quality, movie_id, value

I want to group by date, country, movie, and quality to calculate the SUM of value. But I also want to calculate MIN, MAX, AVG of the torrent_size for all torrents of the movie available on that date.

This is what I got so far

A = FOREACH (GROUP data BY (date, country_code, movie_id, quality)) {
    GENERATE group, SUM(data.value) as total_piracy;
};

However I'm not sure how to do MIN, MAX, AVG size of torrents of the movie available on date disregarding country as well.

Upvotes: 1

Views: 67

Answers (2)

Keshav Pradeep Ramanath
Keshav Pradeep Ramanath

Reputation: 1687

You can check the below commands:

A= LOAD '<locationOfDataSet>' USING PigStorage(',') 
AS (date:chararray, country_code:chararray, torrent_id:int, first_seen:chararray, torrent_size:int, quality:chararray, movie_id:int, value:int) ;
B=GROUP A BY (date,movie_id);
C=FOREACH B GENERATE group, MIN(A.torrent_size), MAX(A.torrent_size),AVG(A.torrent_size);
dump C;

Hope this helps.

Upvotes: 1

nobody
nobody

Reputation: 11080

Create another relation and group by date,movie and get the MIN,MAX,AVG

B = GROUP data BY (date,movie);
C = FOREACH B GENERATE group, 
                       MIN(data.torrent_size) as min_ts,
                       MAX(data.torrent_size) as max_ts,
                       AVG(data.torrent_size) as avg_ts;

Upvotes: 0

Related Questions