在没有循环的情况下计算itab组

时间:2018-06-13 16:11:10

标签: sap abap

我最近在循环中发现了GROUP BY命令。

现在想象下面的例子:

我有一个列categories的itab。我想知道有多少不同的类别。 使用上面链接的GROUP BY语句,我可以计算循环执行的次数。有没有循环的更简单的方法?

1 个答案:

答案 0 :(得分:5)

以下是您可以在系统上尝试的报告中包含的简短示例代码。

REPORT Z_GROUP_COUNT.

  TYPES: BEGIN OF lty_st_for_reduce,
           categories TYPE C LENGTH 4,
         END OF lty_st_for_reduce.

  DATA: lt_for_reduce TYPE STANDARD TABLE OF lty_st_for_reduce.

  APPEND VALUE #( categories = 'ABAP' ) TO lt_for_reduce.
  APPEND VALUE #( categories = 'OBJC' ) TO lt_for_reduce.
  APPEND VALUE #( categories = 'ABAP' ) TO lt_for_reduce.
  APPEND VALUE #( categories = 'ABAP' ) TO lt_for_reduce.
  APPEND VALUE #( categories = 'OBJC' ) TO lt_for_reduce.

  DATA(lv_categories_count) = REDUCE i( INIT count = 0
                                        FOR GROUPS categories OF entry IN lt_for_reduce
                                        GROUP BY ( categories = entry-categories )
                                        NEXT count = count + 1 ).

  " Will output `2`.
  WRITE: lv_categories_count.