Project(Extend(Extend(Project(Extend(Extend(Extend(Extend(Extend(AggregationJoin(Aggregation(??agg1: ?pid, SAMPLE, {}, G1=Group((?pid), Join(Project(Extend(Extend(Extend(AggregationJoin(Aggregation(??agg1: ?pid, SAMPLE, {}, G2=Group((?pid), Join(ToMultiSet({{(?pid, )}, {(?pid, )}, {(?pid, )}, {(?pid, )}, {(?pid, )}}), Extend(Extend(BGP( ?pid ^ ?offid . ?offid ?cond), ?newIncrement, if((?cond = 1), 0, 1) ), ?oldIncrement, if((?cond = 1), 1, 0) )))), Aggregation(??agg2: ?newIncrement, SUM, {}, G2), Aggregation(??agg3: ?oldIncrement, SUM, {}, G2)), ?pid, ??agg1), ?newOff, ??agg2), ?usedOff, ??agg3), {?pid, ?newOff, ?usedOff}), BGP( ?pid ^ ?oid . ?oid ?price)))), Aggregation(??agg2: ?oid, SAMPLE, {}, G1), Aggregation(??agg3: ?price, MIN, {}, G1), Aggregation(??agg4: ?newOff, SAMPLE, {}, G1), Aggregation(??agg5: ?usedOff, SAMPLE, {}, G1)), ?pid, ??agg1), ?oid, ??agg2), ?minPrice, ??agg3), ?newOff, ??agg4), ?usedOff, ??agg5), {?pid, ?oid, ?minPrice, ?newOff, ?usedOff}), ?oldOidFinal, if((?newOff = 0), ?oid, ) ), ?newOidFinal, if((?usedOff = 0), ?oid, ) ), {?minPrice, ?newOff, ?newOidFinal, ?oid, ?oldOidFinal, ?pid, ?usedOff})