defg lastVolPx(price, vol, bound){ cumVol = vol.cumsum() if(cumVol.tail() <= bound) return wavg(price, vol) else{ start = (cumVol <= cumVol.tail() - bound).sum() return wavg(price.subarray(start:), vol.subarray(start:)) } } n = 5000000 t =table(rand(string(1..4000), n) as sym, rand(10.0, n) as price, rand(500, n) as vol) timer select lastVolPx(price, vol, 1000) as lastVolPx from t group by sym