Pork order upon request

grunt> dump jn;

(k1,k4,10)
(k1,k5,15)
(k2,k4,9)
(k3,k4,16)

grunt> jn = group jn by $1;
grunt> dump jn;


(k4,{(k1,k4,10),(k2,k4,9),(k3,k4,16)})
(k5,{(k1,k5,15)})

Now from here I need the following output:

(k4,{(k3,k4,16),(k1,k4,10)})
(k5,{(k1,k5,15)})

Basically, I want to sort by numbers: 10,9,16 and select the top 2 for each row.
How to do it?

+5
source share
1 answer

This is similar to question , and you can use Nested FOREACH , for example:

A = LOAD 'data';
jn = group A by $1;
B = FOREACH jn {
  sorted = ORDER A by $2 ASC;
  lim = LIMIT sorted 2;
  GENERATE lim;
};
DUMP B;
+9
source

All Articles