对分组表进行数学运算 [英] Make math operations on a grouped table

查看:41
本文介绍了对分组表进行数学运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题不是真正的编程语言.

My problem is not in a real programming Language.

我有 ABAP 语言的练习,但语言不是很重要.

I have an exercise in ABAP Language but is not very important the language.

无论如何,我有一张桌子:

Anyway, I have a table:

我需要计算仓位的总成本(显然是在选择之后).然后,该表将按两个字段(MATNR 和 BUKRS)分组,因此我需要知道每个组的总成本 MAX、总成本 MIN 和总成本 AVERAGE.

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.

但是我需要一个简单的算法来解决这个问题(伪代码).

However I need a simple algorithm to solve this problem (pseudo-code).

我希望我说的很清楚.

推荐答案

对于表聚合,我发现 LOOP 中的 AT 函数非常方便.

For table aggregations I find the AT functions within a LOOP very handy.

根据您需要的维度对字段进行排序,并按升序或降序对值进行排序.

Sort your fields according the dimensions you need and sort the values within ascending or descending.

字段的顺序在这里非常重要,因为 AT 会查找指定字段及其在同一行中剩余的所有字段的更改.因此,您可以逐个处理组并将组聚合的结果附加到您的结果表中.

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. 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.

这篇关于对分组表进行数学运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆