Project(Extend(Extend(Project(Extend(Extend(Project(Extend(Extend(Extend(Extend(Extend(Extend(AggregationJoin(Aggregation(??agg1: ?pid, SAMPLE, {}, G1=Group((?pid, ?cond), Join(ToMultiSet({{(?pid, )}, {(?pid, )}, {(?pid, )}, {(?pid, )}, {(?pid, )}}), Extend(Extend(BGP( ?pid ^ ?offid . ?offid ?cond . ?offid ?price), ?newIncrement, if((?cond = 1), 0, 1) ), ?oldIncrement, if((?cond = 1), 1, 0) )))), Aggregation(??agg2: ?cond, SAMPLE, {}, G1), Aggregation(??agg3: ?offid, SAMPLE, {}, G1), Aggregation(??agg4: ?price, MIN, {}, G1), Aggregation(??agg5: ?newIncrement, SUM, {}, G1), Aggregation(??agg6: ?oldIncrement, SUM, {}, G1)), ?pid, ??agg1), ?cond, ??agg2), ?oid, ??agg3), ?minPrice, ??agg4), ?newOffs, ??agg5), ?usedOffs, ??agg6), {?pid, ?cond, ?oid, ?minPrice, ?newOffs, ?usedOffs}), ?usedOid, if((?cond = 0), ?oid, ?null) ), ?newOid, if((?cond = 1), ?oid, ?null) ), {?minPrice, ?cond, ?newOffs, ?usedOffs, ?pid, ?newOid, ?usedOid}), ?finalNewOffs, if((?newOffs = 0), ?null, ?newOffs) ), ?finalUsedOffs, if((?usedOffs = 0), ?null, ?usedOffs) ), {?minPrice, ?cond, ?finalNewOffs, ?finalUsedOffs, ?pid, ?newOid, ?usedOid})