ALV Sample Using Inner Join

REPORT  ZSJC2 NO STANDARD PAGE HEADING.

TYPE-POOLS : SLIS.
DATA       : T_FIELDALV      TYPE SLIS_T_FIELDCAT_ALV.
DATA       : IMPORT_VARIANT  LIKE DISVARIANT.
DATA       : XREPID          LIKE SY-REPID.
DATA       : LAYOUT          TYPE SLIS_LAYOUT_ALV.

TABLES: MARA, MAKT, MVKE.

DATA: BEGIN OF IT_MAT OCCURS 0,
      MATNR LIKE MARA-MATNR,
      MAKTX LIKE MAKT-MAKTX,
      VKORG LIKE MVKE-VKORG,
      KTGRM LIKE MVKE-KTGRM,
      VKORG_2001 LIKE MVKE-VKORG,
      VKORG_2002 LIKE MVKE-VKORG,
      END OF IT_MAT.

DATA: N_2001 TYPE I, N_2002 TYPE I.

*DATA: SNO(4) TYPE N.

SELECTION-SCREEN:  BEGIN OF BLOCK BLK WITH FRAME TITLE TEXT-001.
SELECT-OPTIONS: S_MATNR FOR MARA-MATNR.
SELECTION-SCREEN:  END OF BLOCK BLK.

INITIALIZATION.
  XREPID = SY-REPID.
  PERFORM INITIAL_ALV_FIELDCAT CHANGING T_FIELDALV[].

START-OF-SELECTION.

  IF IT_MAT IS INITIAL.

    SELECT A~MATNR B~MAKTX C~VKORG C~KTGRM INTO CORRESPONDING FIELDS OF TABLE IT_MAT
      FROM ( ( MARA AS A
      INNER JOIN MAKT AS B
      ON B~MATNR = A~MATNR )
      INNER JOIN MVKE AS C
      ON A~MATNR = C~MATNR )
      WHERE A~MATNR IN S_MATNR.
*    ENDSELECT.

  ENDIF.

  CLEAR: N_2001, N_2002.

  LOOP AT IT_MAT.
    IF IT_MAT-VKORG = 2001.
      N_2001 = N_2001 + 1.
      IT_MAT-VKORG_2001 = IT_MAT-KTGRM.
      MODIFY IT_MAT INDEX N_2001 TRANSPORTING VKORG_2001 .

    ELSEIF IT_MAT-VKORG = 2002.
      N_2002 = N_2002 + 1.
      IT_MAT-VKORG_2002 = IT_MAT-KTGRM.
      MODIFY IT_MAT INDEX N_2002 TRANSPORTING VKORG_2002 .
    ENDIF.
  ENDLOOP.

END-OF-SELECTION.

  DATA : GT_EVENT  TYPE SLIS_T_EVENT.

  DATA TITLE(100) TYPE C.
  CLEAR : TITLE.

  SY-TITLE = TITLE.
 

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      I_CALLBACK_PROGRAM = SY-REPID
      IS_LAYOUT          = LAYOUT
      IT_FIELDCAT        = T_FIELDALV[]
      I_SAVE             = 'A'
      I_GRID_TITLE       = 'SALES ORGANIZATION WISE ACCOUNT ASSIGNMENT GROUP'
    TABLES
      T_OUTTAB           = IT_MAT.

  INCLUDE ZSJC2_INITIAL_ALV_FIELDCATF01. 

*----------------------------------------------------------------------*
***INCLUDE ZSJC2_INITIAL_ALV_FIELDCATF01 .
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&      Form  INITIAL_ALV_FIELDCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_T_FIELDALV[]  text
*----------------------------------------------------------------------*
FORM INITIAL_ALV_FIELDCAT  CHANGING FIELDCAT TYPE SLIS_T_FIELDCAT_ALV .

  DATA  : LS_FIELDCAT TYPE SLIS_FIELDCAT_ALV.
  DATA  : CNTR TYPE I VALUE 1.

*  CLEAR ls_fieldcat.
*  cntr = cntr + 1.
*  ls_fieldcat-col_pos = cntr.
*  ls_fieldcat-fieldname = 'SNO'.
*  ls_fieldcat-seltext_l = 'Slno'.
*  APPEND ls_fieldcat TO fieldcat.

  CLEAR LS_FIELDCAT.
  CNTR = CNTR + 1.
  LS_FIELDCAT-COL_POS = CNTR.
  LS_FIELDCAT-FIELDNAME = 'MATNR'.
  LS_FIELDCAT-SELTEXT_L = 'MATERIAL'.
  APPEND LS_FIELDCAT TO FIELDCAT.

  CLEAR LS_FIELDCAT.
  CNTR = CNTR + 1.
  LS_FIELDCAT-COL_POS = CNTR.
  LS_FIELDCAT-FIELDNAME = 'MAKTX'.
  LS_FIELDCAT-SELTEXT_L = 'MATERIAL DESC'.
  APPEND LS_FIELDCAT TO FIELDCAT.

  CLEAR LS_FIELDCAT.
  CNTR = CNTR + 1.
  LS_FIELDCAT-COL_POS = CNTR.
  LS_FIELDCAT-FIELDNAME = 'VKORG_2001'.
  LS_FIELDCAT-SELTEXT_L = 'VKORG_2001'.
  APPEND LS_FIELDCAT TO FIELDCAT.

  CLEAR LS_FIELDCAT.
  CNTR = CNTR + 1.
  LS_FIELDCAT-COL_POS = CNTR.
  LS_FIELDCAT-FIELDNAME = 'VKORG_2002'.
  LS_FIELDCAT-SELTEXT_L = 'VKORG_2002'.
  APPEND LS_FIELDCAT TO FIELDCAT.

  LAYOUT-ZEBRA = 'X'.
*  layout-colwidth_optimize = 'X'.

ENDFORM.                    " INITIAL_ALV_FIELDCAT 

ABAP

See Also:
Help on Conversion From Numbers to Text Amount

More Function Module
Functions / SAP Script / ALV

Tables
Database Table

ABAP Books List
ABAP/4 Certification, Programming, Smartforms, Sapscripts and Object Oriented Programming Books

Smart Forms
SAP Smartforms

ABAP Menu:
ABAP Example Hints and Tips

Return to Index:-
SAP ABAP/4 Programming, Basis Administration, Configuration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.  The site www.gotothings.com is in no way affiliated with SAP AG.
Any unauthorised copying or mirroring is prohibited.