본문 바로가기

SAP/ABAP

ALV로 집계테이블 만들기 (LOOP AT, COLLECT)

최종 작성일: 2024년 5월 31일

 

  • 집계 테이블 만드는 순서

1. 데이터 선언부에 집계 테이블 선언 할것

2. data 불러오는 perform구문에서 select로 가져온 값을 loop를 통해 테이블 -> 행으로 한행씩 가져오고

3. collect문을 통해 한행씩 가져온 값을 집계 테이블에 쌓을 것

 

여기서 유의할 사항은 'display_alv_report.' perform 구문에서 display할 테이블을 집계테이블로 지정해야함

*&---------------------------------------------------------------------*
*& Report ZTASK_7THWEEK
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZTASK_7THWEEK.

TABLES : sflight.


TYPE-POOLS: slis.                                 "ALV Declarations


*Data Declaration
*----------------
TYPES: BEGIN OF t_loop_collect,

carrid type sflight-carrid,
connid type sflight-connid,
price type sflight-price,
currency type sflight-currency,
paymentsum type sflight-paymentsum,

END OF t_loop_collect.

DATA: it_loop_collect TYPE STANDARD TABLE OF t_loop_collect WITH HEADER LINE,
      wa_loop_collect TYPE t_loop_collect,
      wa_loop type t_loop_collect,
      it_loop TYPE STANDARD TABLE OF t_loop_collect WITH HEADER LINE.

*ALV data declarations
DATA: fieldcatalog TYPE slis_t_fieldcat_alv WITH HEADER LINE,
      gd_tab_group TYPE slis_t_sp_group_alv,
      gd_layout    TYPE slis_layout_alv,
      gd_repid     LIKE sy-repid.


DATA : t TYPE slis_t_sp_group_alv .


************************************************************************
*Start-of-selection.
START-OF-SELECTION.

  PERFORM data_retrieval.
  PERFORM build_fieldcatalog.
  PERFORM build_layout.
  PERFORM display_alv_report.


*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCATALOG
*&---------------------------------------------------------------------*
*       Build Fieldcatalog for ALV Report
*----------------------------------------------------------------------*
FORM build_fieldcatalog.

  fieldcatalog-fieldname   = 'carrid'.
  fieldcatalog-seltext_m   = 'carrid'.
  fieldcatalog-col_pos     = 1.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'connid'.
  fieldcatalog-seltext_m   = 'connid'.
  fieldcatalog-col_pos     = 2.
  fieldcatalog-lzero       = 'X'.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'price'.
  fieldcatalog-seltext_m   = 'price'.
  fieldcatalog-col_pos     = 3.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'currency'.
  fieldcatalog-seltext_m   = 'currency'.
  fieldcatalog-col_pos     = 4.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'paymentsum'.
  fieldcatalog-seltext_m   = 'paymentsum'.
  fieldcatalog-col_pos     = 5.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

ENDFORM.                    " BUILD_FIELDCATALOG


*&---------------------------------------------------------------------*
*&      Form  BUILD_LAYOUT
*&---------------------------------------------------------------------*
*       Build layout for ALV grid report
*----------------------------------------------------------------------*
FORM build_layout.

  gd_layout-no_input          = 'X'.
  gd_layout-colwidth_optimize = 'X'.
  gd_layout-zebra = 'X'.
*  gd_layout-info_fieldname =      'LINE_COLOR'.
*  gd_layout-def_status = 'A'.

ENDFORM.                    " BUILD_LAYOUT


*&---------------------------------------------------------------------*
*&      Form  DISPLAY_ALV_REPORT
*&---------------------------------------------------------------------*
*       Display report using ALV grid
*----------------------------------------------------------------------*
FORM display_alv_report.
  gd_repid = sy-repid.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = gd_repid
      is_layout          = gd_layout
      it_fieldcat        = fieldcatalog[]
      i_save             = 'X'
    TABLES
      t_outtab           = it_loop
    EXCEPTIONS
      program_error      = 1
      OTHERS             = 2.
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


ENDFORM.                    " DISPLAY_ALV_REPORT


*&---------------------------------------------------------------------*
*&      Form  DATA_RETRIEVAL
*&---------------------------------------------------------------------*
*       Retrieve data form EKPO table and populate itab it_ekko
*----------------------------------------------------------------------*
FORM data_retrieval.



SELECT carrid connid price currency paymentsum
  from sflight
  into table  it_loop_collect .




  LOOP AT it_loop_collect  into wa_loop_collect .
     COLLECT wa_loop_collect into  it_loop .
ENDLOOP.

SORT it_loop by price.

ENDFORM.                    " DATA_RETRIEVAL

'SAP > ABAP' 카테고리의 다른 글

[WIP]Read Table  (0) 2024.06.04
sort + collect  (0) 2024.06.03
Loop 구문과 Collect 구문의 구조  (0) 2024.05.29
COLLECT 구문  (0) 2024.05.29
Loop 구문  (1) 2024.05.29