본문 바로가기

SAP/ABAP

Left Join & Right Join

작성일: 2024년 5월 20일

목표:  ALV , SELECT-OPTION, SEARCH HELP , JOIN문 까지 파악

 

[LEFT JOIN]

*&---------------------------------------------------------------------*
*& Report ZTEST_TASK_JOIN
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZTEST_TASK_JOIN.


TABLES:     SCARR, spfli, sflight,sbook.

TYPE-POOLS: slis.                                 "ALV Declarations

*Data Declaration   "전역변수
*----------------
TYPES: BEGIN OF t_task_join,

  carrid TYPE SCARR-CARRID,
  CARRNAME TYPE SCARR-CARRNAME,
  CONNID TYPE SPFLI-CONNID,
  CITYFROM TYPE SPFLI-CITYFROM,
  CITYTO TYPE SPFLI-CITYTO,
  FLDATE TYPE sflight-fldate,
  bookid TYPE sbook-bookid,
  customid type sbook-customid,
  custtype type sbook-custtype,

 END OF t_task_JOIN.

DATA: it_task_join TYPE STANDARD TABLE OF t_task_join INITIAL SIZE 0,
      wa_task_join TYPE t_task_join.

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


SELECTION-SCREEN BEGIN OF BLOCK part1 WITH FRAME TITLE text-009.
  SELECT-OPTIONS s_carrid FOR SCARR-carrid.
  SELECT-OPTIONS s_cityfr FOR SPFli-cityfrom.
  SELECT-OPTIONS s_cityto FOR SpfLI-cityto.


  "PARAMETERS p_carrid TYPE scarr-carrid OBLIGATORY DEFAULT 'AA'.
SELECTION-SCREEN END OF BLOCK part1.


INITIALIZATION.




************************************************************************
*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   = 'Airline Code'.
  fieldcatalog-col_pos     =  3.
  "fieldcatalog-outputlen   = 10. "edit mode일때 보통 사용함
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'CARRNAME'.
  fieldcatalog-seltext_m   = 'Airline name'.
  fieldcatalog-lzero = 'X'.
  fieldcatalog-col_pos     = 20.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'connid'.
  fieldcatalog-seltext_m   = 'Flight Connection Number'.
  fieldcatalog-col_pos     = 2.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'CITYFROM'.
  fieldcatalog-seltext_m   = 'Departure city'.
  fieldcatalog-col_pos     = 20.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'CITYTO'.
  fieldcatalog-seltext_m   = 'Arrival city'.
  fieldcatalog-col_pos     = 20.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

    fieldcatalog-fieldname   = 'FLDATE'.
  fieldcatalog-seltext_m   = 'Flight date'.
  fieldcatalog-col_pos     = 8.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

 fieldcatalog-fieldname  ='bookid'.
 fieldcatalog-seltext_m = 'Booking number'.
 fieldcatalog-col_pos = 8.
 APPEND fieldcatalog TO fieldcatalog.
 CLEAR fieldcatalog.

 fieldcatalog-fieldname  ='customid'.
 fieldcatalog-seltext_m = 'customer number'.
 fieldcatalog-col_pos = 8.
 APPEND fieldcatalog TO fieldcatalog.
 CLEAR fieldcatalog.

  fieldcatalog-fieldname  ='custtype'.
 fieldcatalog-seltext_m = 'customer type'.
 fieldcatalog-col_pos = 1.
 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'. "colwidth_optimize를 일단 주고 outlen을 10으로 주는 것이 보통임.
  gd_layout-zebra = 'X'.
  gd_layout-totals_before_items = '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             = 'A'  "빈값 = 저장버튼 안나오게 X=디폴트세팅
    TABLES
      t_outtab           = it_task_join
    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 a~carrid, a~carrname, b~connid, b~cityfrom, b~cityto
  from scarr AS a
  LEFT JOIN spfli as b ON b~CARRID = a~CARRID
  into table @it_task_join
    WHERE a~carrid IN  @s_carrid
  AND  b~cityfrom IN @s_cityfr
  AND  b~cityto  IN @s_cityto.

ENDFORM.                    " DATA_RETRIEVAL
*&---------------------------------------------------------------------*
*& Form DATA_COLLECT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM data_collect .

ENDFORM.

 

 

 

LEFT JOIN이니까 SCARR 테이블과 SPFLI간 매핑되는 값과 SCARR 테이블 에만 있는 값들도 다 출력

 

[Right Join]

 

*&---------------------------------------------------------------------*
*& Report ZTEST_TASK_JOIN
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZTEST_TASK_JOIN.


TABLES:     SCARR, spfli, sflight,sbook.

TYPE-POOLS: slis.                                 "ALV Declarations

*Data Declaration   "전역변수
*----------------
TYPES: BEGIN OF t_task_join,

  carrid TYPE SCARR-CARRID,
  CARRNAME TYPE SCARR-CARRNAME,
  CONNID TYPE SPFLI-CONNID,
  CITYFROM TYPE SPFLI-CITYFROM,
  CITYTO TYPE SPFLI-CITYTO,
  FLDATE TYPE sflight-fldate,
  bookid TYPE sbook-bookid,
  customid type sbook-customid,
  custtype type sbook-custtype,

 END OF t_task_JOIN.

DATA: it_task_join TYPE STANDARD TABLE OF t_task_join INITIAL SIZE 0,
      wa_task_join TYPE t_task_join.

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


SELECTION-SCREEN BEGIN OF BLOCK part1 WITH FRAME TITLE text-009.
  SELECT-OPTIONS s_carrid FOR SCARR-carrid.
  SELECT-OPTIONS s_cityfr FOR SPFli-cityfrom.
  SELECT-OPTIONS s_cityto FOR SpfLI-cityto.


  "PARAMETERS p_carrid TYPE scarr-carrid OBLIGATORY DEFAULT 'AA'.
SELECTION-SCREEN END OF BLOCK part1.


INITIALIZATION.




************************************************************************
*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   = 'Airline Code'.
  fieldcatalog-col_pos     =  3.
  "fieldcatalog-outputlen   = 10. "edit mode일때 보통 사용함
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'CARRNAME'.
  fieldcatalog-seltext_m   = 'Airline name'.
  fieldcatalog-lzero = 'X'.
  fieldcatalog-col_pos     = 20.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'connid'.
  fieldcatalog-seltext_m   = 'Flight Connection Number'.
  fieldcatalog-col_pos     = 2.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'CITYFROM'.
  fieldcatalog-seltext_m   = 'Departure city'.
  fieldcatalog-col_pos     = 20.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'CITYTO'.
  fieldcatalog-seltext_m   = 'Arrival city'.
  fieldcatalog-col_pos     = 20.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

    fieldcatalog-fieldname   = 'FLDATE'.
  fieldcatalog-seltext_m   = 'Flight date'.
  fieldcatalog-col_pos     = 8.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

 fieldcatalog-fieldname  ='bookid'.
 fieldcatalog-seltext_m = 'Booking number'.
 fieldcatalog-col_pos = 8.
 APPEND fieldcatalog TO fieldcatalog.
 CLEAR fieldcatalog.

 fieldcatalog-fieldname  ='customid'.
 fieldcatalog-seltext_m = 'customer number'.
 fieldcatalog-col_pos = 8.
 APPEND fieldcatalog TO fieldcatalog.
 CLEAR fieldcatalog.

  fieldcatalog-fieldname  ='custtype'.
 fieldcatalog-seltext_m = 'customer type'.
 fieldcatalog-col_pos = 1.
 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'. "colwidth_optimize를 일단 주고 outlen을 10으로 주는 것이 보통임.
  gd_layout-zebra = 'X'.
  gd_layout-totals_before_items = '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             = 'A'  "빈값 = 저장버튼 안나오게 X=디폴트세팅
    TABLES
      t_outtab           = it_task_join
    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 a~carrid, a~carrname, b~connid, b~cityfrom, b~cityto
  from scarr AS a
  RIGHT JOIN spfli as b ON b~CARRID = a~CARRID
  into table @it_task_join
    WHERE a~carrid IN  @s_carrid
  AND  b~cityfrom IN @s_cityfr
  AND  b~cityto  IN @s_cityto.

ENDFORM.                    " DATA_RETRIEVAL
*&---------------------------------------------------------------------*
*& Form DATA_COLLECT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM data_collect .

ENDFORM.

 

 

RIGHT JOIN이니까 SPFLI와 SCARR 테이블간 매핑되는 값과 SCARR에는 없지만 SPFLI에 있는 값도 모두 출력

 

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

중첩 Join시 유의 사항  (0) 2024.05.29
중첩 INNER  (0) 2024.05.20
[WIP]Multiple Selection 데이터 흐름  (0) 2024.05.19
Search Help 유형  (0) 2024.05.16
ALV+SearchHelp+SelectOption(1/3)  (0) 2024.05.14