2012-11-14 105 views
1

比方說,我有以下計算SUM

DATA = foreach INPUT { 
    //.. 
    generate group, count(name) as total; 
} 

我會在那裏,關鍵是按名稱

('mike', 'someprop', 10) 
('mike', 'otherprop', 3) 
('doug', 'xprop', 5) 
... 

分組的關係結束了,我想要得到的總和每個名稱的前10名

ALIAS = group DATA by name; 
RESULT = foreach ALIAS { 
    SORTED = ORDER DATA by total desc; 
    TOP10 = LIMIT SORTED 10; 

    //doesn't work! can't have GROUP inside FOREACH 
    AGG = group TOP10 ALL; 
    TOPTOTAL = foreach AGG generate SUM(AGG.total); 

    generate group, TOPTOTAL; 
} 

如何計算一個值(SUM,COUNT,ETC)關係foreach?目前無法在foreach中應用GROUP ALL

回答

0

SUM僅僅是一個函數,它接受一個包作爲它的參數,並且可以通過從TOP10突出創建這個包:

ALIAS = group DATA by name; 
RESULT = foreach ALIAS { 
    SORTED = ORDER DATA by total desc; 
    TOP10 = LIMIT SORTED 10; 
    generate group, SUM(TOP10.total); 
}