我有一些数据如下:
patient_id lab_value
1, 10
1, 3
2, 1
2, 4
3, 5
3, 10
3, 2
我想要做的是找到每个patient_id分组的最大lab_value,然后最终计算lab_value和max之间的差异,如下所示。
patient_id lab_value lab_diff
1, 10, 0
1, 3, 7 (10 - 3)
2, 1, 3
2, 4, 0
3, 5, 5 (10 - 5)
3, 10, 0
3, 2, 8 (10 - 2)
我该怎么做?
答案 0 :(得分:1)
<强>步骤强>
PIG脚本
A = LOAD 'test1.txt' USING PigStorage(',') AS (id:int, lab_value:int);
B = GROUP A BY id;
C = FOREACH B GENERATE group as id,MAX(A.lab_value) as max_value;
C1= DISTINCT C;
D = JOIN A BY id,C1 BY id;
E = FOREACH D GENERATE A::id,A::lab_value,(C1::max_value - A::lab_value) as diff_value;
DUMP E;
<强>结果强>