简体   繁体   English

在分组表上进行数学运算

[英]Make math operations on a grouped table

My problem is not in a real programming Language. 我的问题不是真正的编程语言。

I have an exercise in ABAP Language but is not very important the language. 我有一个ABAP语言练习,但是该语言不是很重要。

Anyway, I have a table: 无论如何,我有一张桌子:

看图片

I need to make the total cost of the position(after the select obviously). 我需要计算该职位的总成本(显然是在选择之后)。 Then, the table will be grouped by two fields (MATNR and BUKRS), so I need to know for each Group the total cost MAX, the total cost MIN and the total cost AVERAGE of the positions. 然后,该表将按两个字段(MATNR和BUKRS)进行分组,因此我需要为每个组知道总成本MAX,总成本MIN和职位的平均成本AVERAGE。

However I need a simple algorithm to solve this problem (pseudo-code). 但是,我需要一个简单的算法来解决此问题(伪代码)。

I hope I was clear. 我希望我很清楚。

For table aggregations I find the AT functions within a LOOP very handy. 对于表聚合,我发现LOOP中的AT函数非常方便。

Sort your fields according the dimensions you need and sort the values within ascending or descending. 根据需要的维度对字段进行排序,并对值进行升序或降序排序。

The order of the fields is very important here, because the AT looks for changes in the specified field and all fields left of it in the same row. 字段的顺序在这里非常重要,因为AT会在指定的字段中查找更改,并将其剩余的所有字段都放在同一行中。 So you handle group for group and append the result of the group aggregation to your result table. 因此,您可以逐组处理分组,并将分组聚合的结果附加到结果表中。

LOOP AT lt_itab ASSIGNING <ls_itab>.
  AT NEW bukrs. "at first entry of combination matnr, bukrs
    ls_agg-matnr = <ls_itab>-matnr.
    ls_agg-bukrs = <ls_itab>-bukrs.
  ENDAT.

  TRY.
    ADD <ls_itab>-amount TO lf_sum.
  CATCH cx_sy_arithmetic_overflow.
     lf_sum = 9999.
  ENDTRY.

  lf_count = lf_count + 1.
  IF <ls_itab>-amount > lf_max.
     lf_max = <ls_itab>-amount.
  ENDIF.

  AT END OF bukrs. "after last entry of combination matnr,bukrs
    ls_agg-avg = lf_sum / lf_count.
    ls_agg-max = lf_max.
    APPEND ls_agg TO lt_agged.
    CLEAR: ls_agg, lf_sum, lf_count, lf_max.
  ENDAT.
ENDLOOP.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM