Randomly sum rows

前端 未结 2 984
梦毁少年i
梦毁少年i 2021-01-17 03:27

I would like to randomly insert in a new temp_table the records from the Initial Table below, grouping them by a new PO number (1234-1, 1234-2,etc..) where each group sum(TK

2条回答
  •  醉梦人生
    2021-01-17 04:11

    It's probably a job for a cursor.

    The algorithm could basically be like this:

    • Collect the rows from the initial table one by one, accumulating sum(TKG) and sum(TVOL):
      • pick out the rows into the temp while the conditions are still met (omit those that exceed either sum);
      • use lineID as the order;
      • iterate up to the end of the list.
    • Upon hitting the end of the table call it a group, then start all over again, omitting the rows that have already been collected into the temp.
    • Continue while there still are rows not collected.

    But I'm too lazy at the moment to give out the actual code, besides it's a homework, and cursors hate me anyway.

提交回复
热议问题